2018-06-22:
, , TypeScript 2.8. . , @krzysztof-kaczor , , TypeScript 2.8 .
, , ( TypeScript 2.4 ), , , :
-, :
type False = '0'
type True = '1'
type Bool = False | True
type IfElse<Cond extends Bool, Then, Else> = {'0': Else; '1': Then;}[Cond];
, , , IfElse<True,A,B> A IfElse<False,A,B> B
Rec<K,V,X>, K V, Rec<K,V,True> , , Rec<K,V,False> :
type Rec<K extends string, V, Required extends Bool> = IfElse<Required, Record<K, V>, Partial<Record<K, V>>>
User DeepPartialUser. UserSchema<R> , , , , R True False:
type UserSchema<R extends Bool> =
Rec<'emailAddress', string, R> &
Rec<'verification', (
Rec<'verified', boolean, R> &
Rec<'verificationCode', string, R>
), R> &
Rec<'activeApps', string[], R>
, ? User DeepPartialUser :
interface User extends UserSchema<True> { }
interface DeepPartialUser extends UserSchema<False> { }
:
var user: User = {
emailAddress: 'foo@example.com',
verification: {
verified: true,
verificationCode: 'shazam'
},
activeApps: ['netflix','facebook','angrybirds']
}
var deepPartialUser: DeepPartialUser = {
emailAddress: 'bar@example.com',
verification: {
verified: false
}
}
. , !