Skip to content

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

Closed
@johnfn

Description

@johnfn

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'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions