-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Closed
Copy link
Labels
In DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
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
dhythm, kyken, jeffrey-dot-li, whzx5byb and 50an6xy06r6n
Metadata
Metadata
Assignees
Labels
In DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript