Skip to content

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

Closed
@Kwongiseok

Description

@Kwongiseok

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

Activity

MartinJohns

MartinJohns commented on May 19, 2022

@MartinJohns
Contributor

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

Kwongiseok commented on May 19, 2022

@Kwongiseok
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??

typescript-bot

typescript-bot commented on May 23, 2022

@typescript-bot
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @MartinJohns@RyanCavanaugh@Kwongiseok@typescript-bot

        Issue actions

          Multiple tagged union with undefined can't catch error · Issue #49176 · microsoft/TypeScript