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

Wrong typecheck for tagged union with deeply placed tag #49443

Closed
cattingcat opened this issue Jun 8, 2022 · 3 comments
Closed

Wrong typecheck for tagged union with deeply placed tag #49443

cattingcat opened this issue Jun 8, 2022 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@cattingcat
Copy link

Bug Report

πŸ•— Version & Regression Information

Stable (v4.7.2) and nightly (v4.8.0-dev.20220608 )

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Kek = {meta: {tag: 'A'}} | {meta: {tag: 'B'}};
type AddKek<S extends Kek> = S extends {meta: {tag: 'A'}} ? S & {payload: number} : S;
type Kek2 = AddKek<Kek>;  // type Kek2 = {meta: {tag: 'A'}, payload: number} | {meta: {tag: 'B'}};


function foo (k: Kek): Kek2 {
    switch (k.meta.tag) {
        case 'A':
            return {
                ...k,
                payload: 1,
            };
        case 'B':
            return k; // impossible to return k, problem with typecheck
    }
}

const k2: Kek2 = {
    meta: {tag: 'A', },
    payload: 1,
};

const k21: Kek2 = {
    meta: {tag: 'B', },
};

πŸ™ Actual behavior

Type 'Kek' is not assignable to type 'Kek2'.
Type '{ meta: { tag: "A"; }; }' is not assignable to type 'Kek2'.
Type '{ meta: { tag: "A"; }; }' is not assignable to type '{ meta: { tag: "A"; }; } & { payload: number; }'.
Property 'payload' is missing in type '{ meta: { tag: "A"; }; }' but required in type '{ payload: number; }'.(2322)
input.tsx(13, 66): 'payload' is declared here.

πŸ™‚ Expected behavior

Typechecks successfully

@cattingcat
Copy link
Author

Instead of case 'B' there could be default

@jcalz
Copy link
Contributor

jcalz commented Jun 8, 2022

You didn't fill out the whole template:

πŸ”Ž Search Terms
What search terms did you use when trying to find an existing bug report?
List them here so people in the future can find this one more easily.

TS doesn't support nested discriminated unions. Duplicate of #18758

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 9, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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