Open Zzzen opened 1 month ago
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
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');
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.
π Search Terms
infer type predicate, destructuring
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?#code/C4TwDgpgBAGlC8UDeVSQFxQOQEEsG4oBDTAZ2ACcBLAOwHMoBfKAH2VXAkywCECoARmUq0GjfACgANhGBQAHqUwwA2gF0EUdZIkB6XVFIALAPYBXKQBMoNE3IgUKJihIDGJmuSgAzKlOAOEJYAjJgoaFzYeIQkhiL0TOqaihJQUAB0vv4OABQ54ZxMAJQIAHwckAjwiLhYRTrunnJZARRBAExhFZG1McLUCYxJiClpmX6tOfJlCukRVTV49RJAA
π» Code
π Actual behavior
error on filtered1
π Expected behavior
not error on filtered1
Additional information about the issue
No response