Skip to content
New issue

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

Destructuring a function parameter that's a union type makes you lose type inference #30702

Closed
sebastiandedeyne opened this issue Apr 2, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@sebastiandedeyne
Copy link

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.

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

@jack-williams
Copy link
Collaborator

This is tracked here #12184.

@sebastiandedeyne
Copy link
Author

Looks like that's the same, closing this!

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 2, 2019
@danielo515
Copy link

It is not the same as #12184 since that doesn't involve destructuring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants