Closed
Description
Bug Report
🔎 Search Terms
destructuring
narrowing
is not assignable to type
🕗 Version & Regression Information
(todo)
- This is a crash
- This changed between versions ______ and _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
💻 Code
This works
type Dastructure =
{b: false, c: null} |
{b: true, c: string}
class Example {
prop?: string;
constructor (options: Dastructure) {
if (options.b) {
this.prop = options.c
}
}
}
This gets an error
type Dastructure =
{b: false, c: null} |
{b: true, c: string}
class Example {
prop?: string;
constructor ({b, c}: Dastructure) {
if (b) {
this.prop = c
// ^^^^^^^^^
// Type 'string | null' is not assignable to type 'string | undefined'.
// Type 'null' is not assignable to type 'string | undefined'.
}
}
}
🙁 Actual behavior
error
🙂 Expected behavior
not error?
I think it's because in the second code the context of {b, c}: A
isn't considered when b
is narrowed