Skip to content

Type inference not working with extends generic argument and keyof and --strictNullChecks #13922

Closed
@Knagis

Description

@Knagis

The title of the bug should probably be improved - I was not able to come up with something that encapsulates the issue.

TypeScript Version: 2.1.5, compiling using --strictNullChecks

This issue seems not to be present in the TypeScript playground (2.1.4-insiders.20161201):

Code

interface Foo {
    foo: number|null;
}

export class Base<T extends Foo> {
    public style: Readonly<T>;

    public styleDirect: Readonly<Foo>;

    public reqNumber(a: number) {
    }

    public reqNumberNull(a: number|null) {
    }

    public method() {
	// works
        this.reqNumberNull(this.style.foo);


	// works
        this.reqNumber(this.styleDirect.foo || 0);


        // does not work
        this.reqNumber(this.style.foo || 0);
    }
}

Expected behavior:

All three method calls should compile correctly.

Actual behavior:

test.ts(26,24): error TS2345: Argument of type 'T["foo"] | 0' is not assignable to parameter of type 'number'.
  Type 'T["foo"]' is not assignable to type 'number'.
    Type 'number | null' is not assignable to type 'number'.
      Type 'null' is not assignable to type 'number'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions