TypeScript Version: 3.2.0-dev.20180929 (First noticed in 3.1.1)
strictNullChecks must be on otherwise this is expected behaviour.
It seems that an issue has been introduced when accessing attributes of a mapped type in a conditional type.
Search Terms:
conditional type undefined extends
mapped type undefined extends
conditional type in mapped type access
Code
type UndefinedKeys<T extends Record<string, any>> = {
[K in keyof T]: undefined extends T[K] ? K : never
};
type MyType = {a: string, b: string | undefined}
type Result1 = UndefinedKeys<MyType>;
const a1: Result1['a'] = 'a'; // $ExpectError
~~ There should be an error here
const b1: Result1['b'] = 'b';
// manually mapping the type, error is show as expected
type Result2 = {
a: undefined extends MyType['a'] ? 'a' : never;
b: undefined extends MyType['b'] ? 'b' : never;
};
const a2: Result2['a'] = 'a'; // $ExpectError
const b2: Result2['b'] = 'b';
To simplify this even more line 5 can be changed to
type MyType = { a: string, b: undefined };
Expected behavior:
- The type
Result1 should be {a: never; b: 'b'}.
- Line 9 should have an error:
Type '"a"' is not assignable to type 'never'.
Actual behavior:
- The type
Result1 is {a: 'a'; b: 'b'}.
- Line 9 has no error.
Playground Link:
Related Issues:
Potentially: #26942
Initially thought it was an issue with Distributive conditional types but the Generic type is in the assignable to position.
TypeScript Version:
3.2.0-dev.20180929(First noticed in3.1.1)strictNullChecksmust be on otherwise this is expected behaviour.It seems that an issue has been introduced when accessing attributes of a mapped type in a conditional type.
Search Terms:
conditional type undefined extends
mapped type undefined extends
conditional type in mapped type access
Code
To simplify this even more line 5 can be changed to
Expected behavior:
Result1should be{a: never; b: 'b'}.Type '"a"' is not assignable to type 'never'.Actual behavior:
Result1is{a: 'a'; b: 'b'}.Playground Link:
strictNullChecksIN OPTIONSRelated Issues:
Potentially: #26942
Initially thought it was an issue with Distributive conditional types but the Generic type is in the assignable to position.