Skip to content

discriminated union using non-literal-type #59151

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

Closed
kim-jaedeok opened this issue Jul 5, 2024 · 2 comments
Closed

discriminated union using non-literal-type #59151

kim-jaedeok opened this issue Jul 5, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@kim-jaedeok
Copy link

kim-jaedeok commented Jul 5, 2024

πŸ”Ž Search Terms

"discriminated union", "discriminated union using non-literal-type"

πŸ•— Version & Regression Information

[email protected]

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAYg9nKBeKBvAUFKBDAXFAZ2ACcBLAOwHMBuTKAI3yLKtoF9bRIoAhbY5Gjp4o5AK4BbehGK0sjUZOmz0HdOi7QAqhTjlB8RAB9e-WugBmY8gGNgpPVGAQiAClQ4ANAyht8O8j0ASiEsUgsoV004COxkJBQAImYKSkSQjCwsbDks+ly2VXQgA

πŸ’» Code

type Foo = {
  a: string;
  b: string;
};
type Bar = {
  a: number;
  b: number;
};

type Uinon = Foo | Bar;

function test({ a, b }: Uinon) {
  if (typeof a === "string") {
    a; // a is string
    b; // b is string | number
  }
}

πŸ™ Actual behavior

Inside the test function, b's type does not narrow down to string when a type is string.
why discriminated union only works with literal type?

πŸ™‚ Expected behavior

i think b's type should be string

Additional information about the issue

https://stackoverflow.com/questions/78707864/type-narrowing-doesnt-work-when-using-a-non-type-literal-field-as-a-tag/78707873#78707873

@jcalz
Copy link
Contributor

jcalz commented Jul 6, 2024

Duplicate of #30506 and others linked within.

Aside: please correct "none-literal" to be "non-literal" so that anyone searching will find it.

@kim-jaedeok kim-jaedeok changed the title discriminated union using none-literal-type discriminated union using non-literal-type Jul 7, 2024
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 9, 2024
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2024
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