Skip to content

Generic lookup type wrongly narrowed to never with strictNullChecks #26130

Closed
@mattmccutchen

Description

@mattmccutchen

I ran across this while migrating my project to strictNullChecks and it's positively bizarre.

TypeScript Version: master (50f442f)

Search Terms: mapped type narrow narrowing null strictNullChecks never

Code (with strictNullChecks)

let mappedObject: {[K in "foo"]: null | {x: string}} = {foo: {x: "hello"}};
declare function foo<T>(x: T): null | T;

function bar<K extends "foo">(key: K) {
  const element = foo(mappedObject[key]);
  if (element == null)
    return;
  const x = element.x;
}

Expected behavior: Compile without error.

Actual behavior: Error: Property 'x' does not exist on type 'never'.

Playground Link: link (remember to enable strictNullChecks)

Related Issues: Didn't find any.

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