-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
It allows to pick properties from a type that may be undefined | ||
@example: | ||
type User = { | ||
id: number; | ||
name: string; | ||
email: string; | ||
} | undefined; | ||
type UserWithId = PickFromPossiblyUndefined<User, 'id' | 'name'>; | ||
Results in: UserWithId = { id: number, name: string } | undefined | ||
@category Object | ||
**/ | ||
export type PickFromPossiblyUndefined<Type, Props extends keyof NonNullable<Type>> = NonNullable<Type> extends object | ||
? Pick<NonNullable<Type>, Props> | ||
Check failure on line 18 in source/pick-from-possibly-undefined.d.ts GitHub Actions / Node.js 20
Check failure on line 18 in source/pick-from-possibly-undefined.d.ts GitHub Actions / Node.js 18
|
||
: NonNullable<Type>; | ||
Check failure on line 19 in source/pick-from-possibly-undefined.d.ts GitHub Actions / Node.js 20
Check failure on line 19 in source/pick-from-possibly-undefined.d.ts GitHub Actions / Node.js 18
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {expectAssignable, expectType} from 'tsd'; | ||
import type {PickFromPossiblyUndefined} from '../index'; | ||
import {expectTypeOf} from 'expect-type'; | ||
Check failure on line 3 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 20
Check failure on line 3 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 18
|
||
|
||
type BillingDetails = { | ||
taxId: string; | ||
Check failure on line 6 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 20
Check failure on line 6 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 18
|
||
companyName: string; | ||
Check failure on line 7 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 20
Check failure on line 7 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 18
|
||
address: string; | ||
Check failure on line 8 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 20
Check failure on line 8 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 18
|
||
bankAccount: string; | ||
Check failure on line 9 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 20
Check failure on line 9 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 18
|
||
ibanBankAccount: string; | ||
Check failure on line 10 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 20
Check failure on line 10 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 18
|
||
} | undefined; | ||
|
||
type CompanyBankAccounts = PickFromPossiblyUndefined<BillingDetails, 'bankAccount' | 'ibanBankAccount'>; | ||
|
||
const bankAccounts: CompanyBankAccounts = { | ||
bankAccount: '123456789', | ||
Check failure on line 16 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 20
Check failure on line 16 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 18
|
||
ibanBankAccount: '123456789', | ||
Check failure on line 17 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 20
Check failure on line 17 in test-d/pick-from-possibly-undefined.ts GitHub Actions / Node.js 18
|
||
}; | ||
|
||
expectAssignable<CompanyBankAccounts>(bankAccounts); | ||
expectType<string>(bankAccounts.bankAccount); | ||
expectType<string>(bankAccounts.ibanBankAccount); | ||
expectTypeOf(bankAccounts).toMatchTypeOf({ | ||
bankAccount: '123456789', | ||
ibanBankAccount: '123456789', | ||
}); |