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: