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

Multiple tagged union with undefined can't catch error #49176

Closed
Kwongiseok opened this issue May 19, 2022 · 3 comments
Closed

Multiple tagged union with undefined can't catch error #49176

Kwongiseok opened this issue May 19, 2022 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@Kwongiseok
Copy link

Bug Report

TypeScript can't catch error about Multiple tagged union (more 3 tagged union) with undefined

🔎 Search Terms

Multiple tagged union , tagged union with undefined , undefined union

🕗 Version & Regression Information

TypeScript Version : 4.6

Please keep and fill in the line that best applies:
-->

  • This is a bug
  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

💻 Code

type T = 1 | 2 | 3

interface P {
  a: T
}
interface A extends P {
  a: 1
  b: undefined
}
interface B extends P {
  a: 2
  b: number
}
interface C extends P {
  a: 3
}
type Q = A | B | C

const a: Q = { a: 1, b: 1 } // Good :  TypeScript catch error, b must be undefined

const c: Q = { a: 3, b: 1 } // Bad : TypeScript didn't catch any error, TS compiler passed it.

🙁 Actual behavior

variable a is error
variable c is not error

🙂 Expected behavior

I expected variable c is error when a is 3 variable b shouldn't exits

@MartinJohns
Copy link
Contributor

MartinJohns commented May 19, 2022

I expected variable c is error when a is 3 variable b shouldn't exits

See: https://github.com/microsoft/TypeScript/wiki/FAQ#what-is-structural-typing

Duplicate of #20863. Generally the excess-property check does not work for union types.

@Kwongiseok
Copy link
Author

type T = 1 | 3

interface P {
  a: T
}
interface A extends P {
  a: 1
  b: undefined
}

interface C extends P {
  a: 3
}

type Q = A | C


const a: Q = {a: 3, b: 4} //  It catch error but type inference is { a: C, b: A}
const b: Q = {a: 1 , b: undefined} // It is Okay  { a: A, b: A }  

I think In this case variable a must should pass type check Okay....

why 2 tagged union is okay??

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label May 20, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants