Skip to content

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

Closed

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.

Activity

  1. changed the title [-]a optional arg of type `Bar[K] | null` can not receive `null`[/-] [+]an optional arg of type `Bar[K] | null` can not receive `null`[/+] on Feb 2, 2017
  2. gdh1995 commented on Feb 2, 2017

    @gdh1995
    ContributorAuthor

    I find a work-around: b?: Bar[K] | null | undefined is OKay. So I wonder if this is working as indended.

  3. sandersn commented on Feb 2, 2017

    @sandersn
    Member

    No, definitely a bug. This is very strange!

  4. sandersn commented on Feb 2, 2017

    @sandersn
    Member

    Here's another repro showing that it's probably instantiation of the index type:

    class C<K extends keyof Bar> {
        p?: Bar[K] | null;
        constructor(name: K) { };
    }
    let ctest = new C("bar");
    ctest.p = null; // <-- error here

    Note that deleting all properties that include the type null (foo, in this example) also makes the error go away.

  5. ahejlsberg commented on Feb 2, 2017

    @ahejlsberg
    Member

    This cause of this rather involved: When we add undefined to a parameter declared with ?, we perform subtype reduction on the resulting union type. So, we construct the type Bar[K] | null | undefined and then subtype reduce that type. That eventually causes us to check whether null is a subtype of Bar[K], which we say is true if null is a subtype of the apparent type of Bar[K]. Since the apparent type of Bar[K] is Bar['bar' | 'foo'], which reduces to number | object | null, we end up concluding it is a subtype and therefore remove null from the parameter type.

    I think the core issue here is that we use the same apparent type algorithm for a lookup type for both reading and writing. We union the types of the properties in the underlying type, which is correct for reading, but we really should use an intersection when checking for writing.

    Anyway, a simple fix is to not do subtype reduction in the the call to getUnionType in the includeFalsyTypes function. There's really no meaningful rationale for the subtype reduction there and the fix has no effect on our baselines.

  6. sandersn commented on Feb 2, 2017

    @sandersn
    Member

    Anders Hejlsberg (@ahejlsberg) do you want me to write up the fix or do you want to do it?

  7. sandersn commented on Feb 7, 2017

    @sandersn
    Member

    Fix is up at #13930

  8. added
    Fixed
    on Feb 7, 2017
  9. vilicvane commented on Feb 27, 2017

    @vilicvane

    Mohamed Hegazy (@mhegazy) Nathan Shively-Sanders (@sandersn) A similar bug still exists for function parameter:

    function foo(bar: string | undefined = '') {
        bar = undefined;
        ^ Type 'undefined' is not assignable to type 'string'.
    }
  10. krisselden commented on Mar 2, 2017

    @krisselden

    possible cause of #14406

  11. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugFixed

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions