Skip to content

Destructuring an optional property with union containing literal type should not be widen if the default value is compatible with the union #35693

@vilicvane

Description

@vilicvane

TypeScript Version: 3.8.0-dev.20191214

Search Terms: union, default value, literal, destructuring, optional

It surprised me a little bit not finding a duplicate myself. This is a frequent issue for us, though most of the time we can "fix" it with type assertion.

Code

interface Foo {
  bar: 'yo' | 'ha' | undefined;
}

let { bar = 'yo' } = {} as Foo;

bar; // expecting `bar` to be `'yo' | 'ha'`, but gets `string` instead

Playground Link: http://www.typescriptlang.org/play/?ts=3.8.0-dev.20191214#code/JYOwLgpgTgZghgYwgAgGIHt3IN4ChnIBGcUAXMgOQCe6FyAPpQBZx2MCuIAJhDKBFwDcuAL65cAGwhgcREsgC8lGnRGKcauAGc0mYbmJRByAPQnkEAB4AHCAjCgA5sgAGhl8jBZCKF9VoMzKwuADRE7DKO0jouWmBQTh6gcRBwXEA

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions