Add SetNullable type#1356
Conversation
3fa66de to
f59d605
Compare
som-sm
left a comment
There was a problem hiding this comment.
PR LGTM, just one tiny question.
| declare const variation4: SetNullable<{a: number | null; b: string}, 'a'>; | ||
| expectType<{a: number | null; b: string}>(variation4); | ||
|
|
||
| // Fail if type changes even if nullable is right. |
There was a problem hiding this comment.
WDYM by this? What's failing here?
|
Also, maybe we should remove the Currently, the JSDoc says:
But this is not true for unions: type T = SetNullable<{a: number; b: number} | {a: string; c: string}>;
//=> {a: number | null; b: number} | {a: string | null; c: string}And it's not that this behavior cannot be achieved. It can be achieved by using type T = SetNullable<{a: number; b: number} | {a: string; c: string}, any>;
//=> {a: number | null; b: number | null} | {a: string | null; c: string | null}So even if we keep the |
|
I think we should use We should probably make the same change to |
Closes #697
Adds
SetNullable<BaseType, Keys>— the inverse ofSetNonNullable. Makes specified keys nullable by adding| nullto their value types, while preservingreadonlyand optional modifiers.If no keys are specified, all keys are made nullable (matching
SetNonNullable's default behavior).Implementation mirrors
SetNonNullableusing a homomorphic mapped type.