Skip to content

Typeguard not working if checked against ExcludeΒ #49118

Closed

Description

Bug Report

πŸ”Ž Search Terms

typeguard exclude union

πŸ•— Version & Regression Information

v4.6.2

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about typeguards

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type AAA = {
    c: 0
    b: 1
} | {
    c: Exclude<number, 0>
    x: 1
}

const a = {} as unknown as AAA

if (a.c !== 0) a.x

πŸ™ Actual behavior

error

Property 'x' does not exist on type 'AAA'.
  Property 'x' does not exist on type '{ c: 0; b: 1; }'.

πŸ™‚ Expected behavior

no error

Activity

  1. MartinJohns commented on May 15, 2022

    @MartinJohns
    Contributor

    The type Exclude<number, 0> is exactly the same type as number. It does not do what you think it does. There is no way to represent a type "any number, except 0" in TypeScript. This would first require #29317.

  2. qwelias commented on May 15, 2022

    @qwelias
    Author

    thanks, tho it's been open for 3+yrs now...

  3. MartinJohns commented on May 16, 2022

    @MartinJohns
    Contributor

    It's a fairly difficult issue to solve.

  4. locked as resolved and limited conversation to collaborators on Oct 22, 2025
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions