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

Nullable types can't be reconciled when they are parameterized keys #21182

Closed
johnfn opened this issue Jan 14, 2018 · 2 comments
Closed

Nullable types can't be reconciled when they are parameterized keys #21182

johnfn opened this issue Jan 14, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@johnfn
Copy link

johnfn commented Jan 14, 2018

TypeScript Version: 2.6.2

Code

type Rect = {
  width: number | undefined;
}

class HasARect<T extends Rect> {
  rect: T;

  get<K extends keyof T>(prop: K): T[K] {
    return this.rect[prop];
  }

  getWidth(r: T): number {
    return this.get("width") || 0; // Type 'undefined' is not assignable to type 'number'.
  }
}

Expected behavior:

This is not a type error.

Actual behavior:

Type 'T["width"]' is not assignable to type 'number'.
  Type 'number | undefined' is not assignable to type 'number'.
    Type 'undefined' is not assignable to type 'number'.
@mhegazy
Copy link
Contributor

mhegazy commented Jan 17, 2018

narrowing does not occur on generic types, we need a higher order type operator to be applied here instead. see #15376 (comment) for more info.

duplicate of #14366

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jan 17, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants