Skip to content

Destructuring causes infered type amnesia #17023

@HyphnKnight

Description

@HyphnKnight

TypeScript Version: 2.4.0

Code

type foo = {
  bar: number | null;
}

const aFoo: foo = { bar: 3 };

if (aFoo.bar) {
  const { bar } = aFoo;
  const right: number = aFoo.bar;
  const wrong: number = bar; // error : Type 'number | null' is not assignable to type 'number'
}

Expected behavior:
bar has the type number.

Actual behavior:
Forgets that we checked and gives bar its original type value: number | null.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions