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
interfaceA{details1?: {y:string};details2?: {x:string};}functioncreate<PropNameextendskeyofA>(name: PropName,a: A): Exclude<A[PropName],undefined>{constx: A[PropName]=a[name];if(x)returnx;// Type error: Type 'A[PropName]' is not assignable to type 'Exclude<A[PropName], undefined>'.thrownewError();}
🙁 Actual behavior
TypeScript complains that x in if (x) return x; is of type 'A[PropName]' and is not assignable to type 'Exclude<A[PropName], undefined>', because it assumes that x can be undefined.
🙂 Expected behavior
I would have expected that this works, as return x is the consequence of the conditional if (x), so x cannot be undefined.
Thanks for taking the time to look at this!
The text was updated successfully, but these errors were encountered:
Bug Report
🔎 Search Terms
index type, indexed access type, index access operator, extends, Exclude, conditional type
🕗 Version & Regression Information
This is the behavior in every version I tried (3.3.3 to 4.3.2), and I reviewed the FAQ for entries about indexed access types.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
TypeScript complains that
x
inif (x) return x;
is of type 'A[PropName]' and is not assignable to type 'Exclude<A[PropName], undefined>', because it assumes thatx
can be undefined.🙂 Expected behavior
I would have expected that this works, as
return x
is the consequence of the conditionalif (x)
, sox
cannot beundefined
.Thanks for taking the time to look at this!
The text was updated successfully, but these errors were encountered: