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
{{ message }}
Repository navigation
Destructuring breaks tagged union case inference #23613
typeMessage={type: 'sendMessageToThread',data: {threadId: number,message: string}}|{type: 'createThread',data: {userIds: number[]}}|{type: 'addUserToThread',data: {threadId: number,userId: number}}// Good casefunctiona(message: Message){switch(message.type){case'sendMessageToThread':
console.log(message.data.threadId)}}// Bad casefunctionb({ type, data }: Message){switch(type){case'sendMessageToThread':
// Error: Property 'threadId' does not exist on typeconsole.log(data.threadId)}}
Expected behavior:
TS should infer which case the tagged union falls into regardless of whether or not the parameter is destructured. data should be inferred as { threadId: number, message: string }.
Actual behavior:
data is inferred as the union of all data values in Message.
Thanks for digging up those issues Mohamed Hegazy (@mhegazy). This seems like a real issue, so I'm not sure why those were closed out. Maybe it's worth keeping one of them open to track the issue? This is pretty surprising behavior from a user's point of view.
Activity
mhegazy commentedon Apr 23, 2018
Duplicate of #14630, and #15482
bcherny commentedon Apr 23, 2018
Thanks for digging up those issues Mohamed Hegazy (@mhegazy). This seems like a real issue, so I'm not sure why those were closed out. Maybe it's worth keeping one of them open to track the issue? This is pretty surprising behavior from a user's point of view.
mhegazy commentedon Apr 23, 2018
I would merge this in #9998. these are design limitation for how control flow analysis is implemented.
bcherny commentedon Apr 28, 2018
Sounds good to me.