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

Discriminated union does not work with string literal types #37650

Closed
alex-kinokon opened this issue Mar 27, 2020 · 3 comments
Closed

Discriminated union does not work with string literal types #37650

alex-kinokon opened this issue Mar 27, 2020 · 3 comments
Assignees
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed Needs Investigation This issue needs a team member to investigate its status.

Comments

@alex-kinokon
Copy link

alex-kinokon commented Mar 27, 2020

TypeScript Version: 3.8.3

Search Terms:

Code

const $type: "@type" = "@type"
type A = { ["@type"]: "A", a: boolean }
type B = { ["@type"]: "B", b: boolean }
type C = A | B

declare const c: C;
switch (c[$type]) {
    case "A":
        c.a; // <-- Property 'a' does not exist on type 'B'.(2339)
}

switch (c["@type"]) {
    case "A":
        c.a;
}

Expected behavior:
No error.

Actual behavior:
Property 'a' does not exist on type 'B'.(2339)

Playground Link:
Playground

Related Issues:
Can’t find any so far.

@RyanCavanaugh
Copy link
Member

@sandersn design limitation?

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Apr 1, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.0 milestone Apr 1, 2020
@sandersn sandersn added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Apr 1, 2020
@sandersn
Copy link
Member

sandersn commented Apr 1, 2020

Performance limitation; it's too expensive to put a control flow node on every element access -- creating it uses too much space and walking over it uses too much time. The decision to create a flow node is made syntactically, just after parsing, so there's no type information to guide it.

We have made similarly expensive expansions to control flow analysis before, like the nodes we add to support assert narrowing. But this feature isn't as useful, I think.

@sandersn sandersn closed this as completed Apr 1, 2020
@sandersn
Copy link
Member

sandersn commented Apr 1, 2020

Discussion is here, including a link back to the original bug: #31478

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

3 participants