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

functions with destructuring parameters are not inferred as type predicate correctly #59497

Open
Zzzen opened this issue Aug 1, 2024 · 3 comments · May be fixed by #59501
Open

functions with destructuring parameters are not inferred as type predicate correctly #59497

Zzzen opened this issue Aug 1, 2024 · 3 comments · May be fixed by #59501
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Zzzen
Copy link
Contributor

Zzzen commented Aug 1, 2024

🔎 Search Terms

infer type predicate, destructuring

🕗 Version & Regression Information

  • This changed between versions 5.5
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAGlC8UDeVSQFxQOQEEsG4oBDTAZ2ACcBLAOwHMoBfKAH2VXAkywCECoARmUq0GjfACgANhGBQAHqUwwA2gF0EUdZIkB6XVFIALAPYBXKQBMoNE3IgUKJihIDGJmuSgAzKlOAOEJYAjJgoaFzYeIQkhiL0TOqaihJQUAB0vv4OABQ54ZxMAJQIAHwckAjwiLhYRTrunnJZARRBAExhFZG1McLUCYxJiClpmX6tOfJlCukRVTV49RJAA

💻 Code

type X = { type: 'A'; a: string } | { type: 'B'; b: string };
let xs: X[] = [];

// should not error
const filtered1: { type: 'A'; a: string }[] = xs
  .filter(({ type }) => type === 'A');

const filtered2: { type: 'A'; a: string }[] = xs
  .filter(x => x.type === 'A');

🙁 Actual behavior

error on filtered1

🙂 Expected behavior

not error on filtered1

Additional information about the issue

No response

@Andarist
Copy link
Contributor

Andarist commented Aug 1, 2024

That's (partly) because there is no identifier to use there:

const filtered1: { type: 'A'; a: string }[] = xs
  .filter(({ type }): ??? is { type: "A"; a: string; } => type === 'A');

One could be generated though but it would slightly go against the recent "reuse as much as possible from the source file" goal. It would require replacing the whole binding pattern with an auto-generated identifier

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Aug 1, 2024
@miguel-leon
Copy link

But there is an identifier there: type.
It would be beautiful for the predicate to be:

const filtered1: { type: 'A'; a: string }[] = xs
  .filter(({ type }): type is "A" => type === 'A');

@Andarist
Copy link
Contributor

Sure, but then you'd have to make this a valid type predicate because it errors today with:

A type predicate cannot reference element 'type' in a binding pattern.(1230)

Not impossible but it asks new questions about the design of how this should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants