We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript Version: 3.4.1
Search Terms: destructure destructuring parameters union
Code
type Action = | { type: "FOO", value: "foo" } | { type: "BAR", value: "bar" }; function doSomethingWithAction(action: Action) { if (action.type === "FOO") { // TypeScript knows `action.value` is "foo" return action.value; } if (action.type === "BAR") { // TypeScript knows `action.value` is "bar" return action.value; } return ""; } function doSomethingWithActionDestructured({ type, value }: Action) { if (type === "FOO") { // TypeScript thinks `value` is "foo" | "bar" return value; } if (type === "BAR") { // TypeScript thinks `value` is "foo" | "bar" return value; } return ""; }
Expected behavior:
Destructuring the object in the parameter should correctly infer the value of value inside the if clauses.
value
if
Actual behavior:
TypeScript can only infer the values when you don't destructure the action parameter.
action
Playground Link: https://www.typescriptlang.org/play/index.html#src=type%20Action%20%3D%0D%0A%20%20%20%20%7C%20%7B%20type%3A%20%22FOO%22%2C%20value%3A%20%22foo%22%20%7D%0D%0A%20%20%20%20%7C%20%7B%20type%3A%20%22BAR%22%2C%20value%3A%20%22bar%22%20%7D%3B%0D%0A%0D%0Afunction%20doSomethingWithAction(action%3A%20Action)%20%7B%0D%0A%20%20%20%20if%20(action.type%20%3D%3D%3D%20%22FOO%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20TypeScript%20knows%20%60action.value%60%20is%20%22foo%22%0D%0A%20%20%20%20%20%20%20%20return%20action.value%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20if%20(action.type%20%3D%3D%3D%20%22BAR%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20TypeScript%20knows%20%60action.value%60%20is%20%22bar%22%0D%0A%20%20%20%20%20%20%20%20return%20action.value%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20return%20%22%22%3B%0D%0A%7D%0D%0A%0D%0Afunction%20doSomethingWithActionDestructured(%7B%20type%2C%20value%20%7D%3A%20Action)%20%7B%0D%0A%20%20%20%20if%20(type%20%3D%3D%3D%20%22FOO%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20TypeScript%20thinks%20%60value%60%20is%20%22foo%22%20%7C%20%22bar%22%0D%0A%20%20%20%20%20%20%20%20return%20value%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20if%20(type%20%3D%3D%3D%20%22BAR%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20TypeScript%20thinks%20%60value%60%20is%20%22foo%22%20%7C%20%22bar%22%0D%0A%20%20%20%20%20%20%20%20return%20value%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20return%20%22%22%3B%0D%0A%7D
Related Issues: None found
The text was updated successfully, but these errors were encountered:
This is tracked here #12184.
Sorry, something went wrong.
Looks like that's the same, closing this!
It is not the same as #12184 since that doesn't involve destructuring
No branches or pull requests
TypeScript Version: 3.4.1
Search Terms: destructure destructuring parameters union
Code
Expected behavior:
Destructuring the object in the parameter should correctly infer the value of
value
inside theif
clauses.Actual behavior:
TypeScript can only infer the values when you don't destructure the
action
parameter.Playground Link: https://www.typescriptlang.org/play/index.html#src=type%20Action%20%3D%0D%0A%20%20%20%20%7C%20%7B%20type%3A%20%22FOO%22%2C%20value%3A%20%22foo%22%20%7D%0D%0A%20%20%20%20%7C%20%7B%20type%3A%20%22BAR%22%2C%20value%3A%20%22bar%22%20%7D%3B%0D%0A%0D%0Afunction%20doSomethingWithAction(action%3A%20Action)%20%7B%0D%0A%20%20%20%20if%20(action.type%20%3D%3D%3D%20%22FOO%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20TypeScript%20knows%20%60action.value%60%20is%20%22foo%22%0D%0A%20%20%20%20%20%20%20%20return%20action.value%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20if%20(action.type%20%3D%3D%3D%20%22BAR%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20TypeScript%20knows%20%60action.value%60%20is%20%22bar%22%0D%0A%20%20%20%20%20%20%20%20return%20action.value%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20return%20%22%22%3B%0D%0A%7D%0D%0A%0D%0Afunction%20doSomethingWithActionDestructured(%7B%20type%2C%20value%20%7D%3A%20Action)%20%7B%0D%0A%20%20%20%20if%20(type%20%3D%3D%3D%20%22FOO%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20TypeScript%20thinks%20%60value%60%20is%20%22foo%22%20%7C%20%22bar%22%0D%0A%20%20%20%20%20%20%20%20return%20value%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20if%20(type%20%3D%3D%3D%20%22BAR%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20TypeScript%20thinks%20%60value%60%20is%20%22foo%22%20%7C%20%22bar%22%0D%0A%20%20%20%20%20%20%20%20return%20value%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20return%20%22%22%3B%0D%0A%7D
Related Issues: None found
The text was updated successfully, but these errors were encountered: