Skip to content

an optional arg of type Bar[K] | null can not receive null #13826

@gdh1995

Description

@gdh1995

TypeScript Version: 2.2.0-dev.20170130 (npm version) / master

Code

interface Bar {  bar: number;  foo: object | null;  }

let a = {
  test<K extends keyof Bar> (a: K,  b?: Bar[K]  |  null)  {
    console.log(a, b);
  }
};
a.test("bar", null);

Expected behavior:
tsc --strictNullChecks compiles it without errors.

Actual behavior:
It reports:

error TS2345: Argument of type 'null' is not assignable to parameter of type 'number | undefin
ed'.

Notes
This case only occurs if at least one member of Bar can be null and --strictNullChecks is enabled.

I think the checker should not merge null and undefined in optional union-type args.

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