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

Bug with union types #25958

Closed
johnfn opened this issue Jul 26, 2018 · 3 comments
Closed

Bug with union types #25958

johnfn opened this issue Jul 26, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@johnfn
Copy link

johnfn commented Jul 26, 2018

TypeScript Version: 3.1.0-dev.20180714

Search Terms:

union

Code

function g(x: "a" | "b") {
    const f: { type: "a" } | { type: "b" } = {
        type: x
    };
}

Expected behavior:

No error.

Actual behavior:

Type '"a"' is not assignable to type '"b"'.

Playground Link:

http://www.typescriptlang.org/play/index.html#src=function%20g(x%3A%20%22a%22%20%7C%20%22b%22)%20%7B%0A%20%20%20%20const%20f%3A%20%7B%20type%3A%20%22a%22%20%7D%20%7C%20%7B%20type%3A%20%22b%22%20%7D%20%3D%20%7B%0A%20%20%20%20%20%20%20%20type%3A%20x%0A%20%20%20%20%7D%3B%0A%7D%0A

Related Issues:

I'm pretty sure this already exists, but I have no clue what to search for. :(

@AlCalzone
Copy link
Contributor

Does this work for you?

function g<T extends "a" | "b">(x: T) {
    const f: { type: T } = {
        type: x
    };
    return f;
}

const a = g("a"); // { type: "a" }
const b = g("b"); // { type: "b" }
const c = g("c"); // error

@jcalz
Copy link
Contributor

jcalz commented Jul 26, 2018

Duplicate of #12052

@ghost ghost added the Duplicate An existing issue was already created label Jul 26, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

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