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
The compiler complains that on the line where needsId is called, string | undefined cannot be assigned to string.
π Expected behavior
Inside the if-statement, the Typescript compiler knows that slot.id is a string. I expect slot to be narrowed to Slot & { id: string } inside the if-statement, so that needsId(slot) is a valid call.
The text was updated successfully, but these errors were encountered:
Type guards do not propagate type narrowings to parent objects. The narrowing is only applied upon access of the narrowed property which is why the destructing function works, but the reference function does not. Narrowing the parent would involve synthesizing new types which would be expensive. More detail in this comment.
Bug Report
π Search Terms
Type narrowing optional property
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
The compiler complains that on the line where
needsId
is called,string | undefined
cannot be assigned tostring
.π Expected behavior
Inside the if-statement, the Typescript compiler knows that
slot.id
is astring
. I expectslot
to be narrowed toSlot & { id: string }
inside the if-statement, so thatneedsId(slot)
is a valid call.The text was updated successfully, but these errors were encountered: