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

Generated param matcher does not support inferred type predicates #2654

Open
hyunbinseo opened this issue Jan 7, 2025 · 1 comment
Open

Comments

@hyunbinseo
Copy link

hyunbinseo commented Jan 7, 2025

Describe the bug

param: string is required for inferred type predicates to work

// src/params/fruit.ts

// const match: (param: string) => boolean
export const match = (param) => param === 'apple' || param === 'orange';

// const match: (param: string) => param is "apple" | "orange"
export const match = (param: string) => param === 'apple' || param === 'orange';

Reproduction

hyunbinseo/svelte-kit-13279@ae0d717

Logs

No response

System Info

System:
  OS: macOS 15.2
  CPU: (10) arm64 Apple M1 Pro
  Memory: 81.66 MB / 16.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 22.12.0 - ~/.local/state/fnm_multishells/21185_1736236050279/bin/node
  npm: 10.9.0 - ~/.local/state/fnm_multishells/21185_1736236050279/bin/npm
  pnpm: 9.15.2 - ~/.local/state/fnm_multishells/21185_1736236050279/bin/pnpm
Browsers:
  Chrome: 131.0.6778.205
  Edge: 131.0.2903.112
  Safari: 18.2
npmPackages:
  @sveltejs/adapter-auto: ^3.0.0 => 3.3.1 
  @sveltejs/kit: ^2.0.0 => 2.15.1 
  @sveltejs/vite-plugin-svelte: ^4.0.0 => 4.0.4 
  svelte: ^5.0.0 => 5.16.5 
  vite: ^5.4.11 => 5.4.11

Severity

annoyance

Additional Information

No response

@dummdidumm dummdidumm transferred this issue from sveltejs/kit Jan 7, 2025
@dummdidumm
Copy link
Member

For context: This is a zero types specific issue (we augment the match function with types). We add both a string type and a return type, e.g. export const match = (param) => param === 'apple' || param === 'orange'; becomes export const match = (param: string): boolean => param === 'apple' || param === 'orange';, which is why inference does not work.

This is so that the return type is also checked for correctness.

What we can do is to use satisfies instead when using the export const match = ... variant, but we cannot change it for the function variant (export function match(... because then we wouldn't be able to check the return type anymore.

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

2 participants