Skip to content

undefined extends all types when accessing a generic in a mapped typed #27470

Closed
@squirly

Description

@squirly

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:

  1. The type Result1 should be {a: never; b: 'b'}.
  2. Line 9 should have an error: Type '"a"' is not assignable to type 'never'.

Actual behavior:

  1. The type Result1 is {a: 'a'; b: 'b'}.
  2. Line 9 has no error.

Playground Link:

TypeScript Version Playground Link
2.8.1 Playground
3.0.1 Playground
3.1.6 Playground
3.2.1 Playground
3.2.1-insiders.20181128 (Official Playground) Playground <---- ENABLE strictNullChecks IN OPTIONS
3.3.0-dev.20190104 Fixed?

Related Issues:
Potentially: #26942

Initially thought it was an issue with Distributive conditional types but the Generic type is in the assignable to position.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions