You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
error TS2322: Type '{ a: string; b: string; c: string | null; }' is not assignable to type 'Y'.
Types of property 'c' are incompatible.
Type 'string | null' is not assignable to type 'string'.
Type 'null' is not assignable to type 'string'.
20 const y: Y = {
~
When explicitly passing the field again, it works:
See above, error is thrown although the "problematic" property was narrowed down to be compatible with the target type in the assignment.
🙂 Expected behavior
Should not throw a type error and should not require the workaround in the above code example with the explicit assignment from the narrowed-down field: c: x.c
The text was updated successfully, but these errors were encountered:
Narrowing individual properties of an object won’t narrow the containing type except in certain situations, because it’s generally difficult (from a technical standpoint) to do so; if this were possible in general we wouldn’t need to have discriminated unions.
Bug Report
It appears, that when a type is narrowed, the narrowing is ignored when assigning to a technically compatible type.
I skimmed the issue tracker rather quickly, I hope I haven't missed an already known issue with a ticket.
🔎 Search Terms
🕗 Version & Regression Information
TypeScript v4.3.2 & v.4.5.2
This is the behavior in every version I tried, and I reviewed the FAQ for entries about 10 minutes
⏯ Playground Link
Playground link with relevant code
💻 Code
Happens when:
When explicitly passing the field again, it works:
🙁 Actual behavior
See above, error is thrown although the "problematic" property was narrowed down to be compatible with the target type in the assignment.
🙂 Expected behavior
Should not throw a type error and should not require the workaround in the above code example with the explicit assignment from the narrowed-down field:
c: x.c
The text was updated successfully, but these errors were encountered: