Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control flow analysis inference of a generic mapped type #27945

Closed
JasonKleban opened this issue Oct 17, 2018 · 3 comments
Closed

Control flow analysis inference of a generic mapped type #27945

JasonKleban opened this issue Oct 17, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@JasonKleban
Copy link

When upgrading to 3.1.3 I get a new error not in 3.0.3:

I see two problems in the type checking of the sample below.

  1. before const lo: number = min!;, the return after null test of either identifier means that both will be non-null.
  2. Even so, I'm asserting non-null and still it thinks it could be null. The subsequent acceptance of lo2 which includes null as an option suggests that it isn't a problem with the T["min"] and number half.

(There is no error without the generic aspect of this sample.)

interface RangeSelectorStateBase {
    min: number | null;
    max: number | null;
}

type RangeSelectorState<T> = {
    [P in keyof (T & RangeSelectorStateBase)]: (T & RangeSelectorStateBase)[P];
}

function foo<T>(state : RangeSelectorState<T>) {
    const { min, max } = state;

    if (min === null || max === null) {
        return;
    }

    const lo: number = min!; // Type 'T["min"] & null' is not assignable to type 'number'.
    const hi: number = max!; // Type 'T["min"] & null' is not assignable to type 'number'.

    const lo2: number | null = min; // type-checks fine.
    const hi2: number | null = max; // type-checks fine.
}

playground link

Full error text:

Type 'NonNullable<(T & RangeSelectorStateBase)["min"]>' is not assignable to type 'number'.
  Type '(T["min"] & null) | (T["min"] & number)' is not assignable to type 'number'.
    Type 'T["min"] & null' is not assignable to type 'number'.
      Type 'NonNullable<T["min"] & null> | NonNullable<T["min"] & number>' is not assignable to type 'number'.
        Type 'NonNullable<T["min"] & null>' is not assignable to type 'number'.
          Type 'T["min"] & null' is not assignable to type 'number'.
@weswigham weswigham added Bug A bug in TypeScript Needs Investigation This issue needs a team member to investigate its status. Domain: Conditional Types The issue relates to conditional types labels Oct 17, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.2 milestone Oct 25, 2018
@DanielRosenwasser
Copy link
Member

Seems potentially related to the recent indexed access type changes.

@ahejlsberg
Copy link
Member

This looks to be the same underlying issue as #22137. Assigning to @weswigham.

@ahejlsberg ahejlsberg assigned weswigham and unassigned ahejlsberg Feb 6, 2019
@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. Bug A bug in TypeScript Domain: Conditional Types The issue relates to conditional types labels Mar 7, 2019
@RyanCavanaugh
Copy link
Member

Tracking with #22137

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants