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

narrowing doesn't work when spreading from ternary operator #45769

Open
DetachHead opened this issue Sep 7, 2021 · 2 comments
Open

narrowing doesn't work when spreading from ternary operator #45769

DetachHead opened this issue Sep 7, 2021 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@DetachHead
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

narrow spread ternary

πŸ•— Version & Regression Information

v4.5.0-dev.20210907

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface Foo {
  type: "foo"
  foo: string
}

interface Bar {
  type: "bar"
  bar: number
}

declare const thing: Foo|Bar

const otherThing: Foo|Bar = { //error: Type '"foo" | "bar"' is not assignable to type '"foo"'.
  type: thing.type,
  ...(thing.type === "foo" ? { foo: thing.foo } : { bar: thing.bar })
}

πŸ™ Actual behavior

error: Type '"foo" | "bar"' is not assignable to type '"foo"'.

πŸ™‚ Expected behavior

no error, since it's not possible for the object to not match either the Foo or Bar types

@MartinJohns
Copy link
Contributor

Related: #19513

@andrewbranch
Copy link
Member

Reminds me a bit of #30581, but more difficult to tell that we’re in that kind of scenario. It seems extremely difficult for us to do better here.

@andrewbranch andrewbranch added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants