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

P.when doesn't work with .exhaustive() when type guard input doesn't match #187

Open
migueloller opened this issue Aug 14, 2023 · 0 comments

Comments

@migueloller
Copy link

Describe the bug

This does not fail, which is a bug:

function isNumber(value: number | string): value is number {
  return typeof value === 'number'
}

const val = 1 as number | string | null

match([val])
  .with([P.when(isNumber)], () => 'number')
  .exhaustive()

This does fail, which shows the desired behavior:

function isNumber(value: unknown): value is number {
  return typeof value === 'number'
}

const val: number | null = 1

match([val as number | null])
  .with([P.when(isNumber)], () => 'number')
  .exhaustive() // type error

This also fails since the input satisfies the parameter in isNumber

function isNumber(value: number | string | null): value is number {
  return typeof value === 'number'
}

const val: number | null = 1

match([val as number | null])
  .with([P.when(isNumber)], () => 'number')
  .exhaustive() // type error

Code Sandbox with a minimal reproduction case
https://codesandbox.io/s/jovial-thunder-nsg2gc

Versions

  • TypeScript version: 5.1.6
  • ts-pattern version: 5.0.5
  • environment: N/A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant