You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be great to avoid the ceremony of having to declare separate type guards for functions accepting a type guard as an argument.
Consider functions like Array.filter or Observable.filter being used to filter a union type with discriminant:
type FooBar = { type: 'foo' } | { type: 'bar' };
const isFoo = <(fb: FooBar) => fb is { type: 'foo'}>(fb => fb.type == "foo");
const fooBar$: Observable<FooBar> = { /* ... */ };
const foo$ = fooBar$.filter(isFoo); //We can use the type guard 'isFoo' to narrow the type to Foo.
However this is quite a lot of wiring for something that anywhere else is quite straight forward. In an if/else or switch block, a type guard isn't necessary... Typescript infers the type from the predicate:
Having to declare the type guard also requires us to 'repeat' out the shape of the type which can be quite painful if it isn't a type alias or class... i.e. we have to explicitly write `fb is { type: 'foo' }.
Typescript should be able to infer that the correct type is { type: 'foo' } from the supplied predicate.
In such an ideal world, we should be able to simply write:
If Typescript requires us to explicitly opt-in the function as a type-guard, then it should be possible to do so without explicitly repeating the type in the declaration. Maybe something like:
To clarify, I'm looking for a general solution that doesn't require explicit definition of the narrowed guard type, not just an overload to Array.filter...
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
It would be great to avoid the ceremony of having to declare separate type guards for functions accepting a type guard as an argument.
Consider functions like
Array.filter
orObservable.filter
being used to filter a union type with discriminant:However this is quite a lot of wiring for something that anywhere else is quite straight forward. In an if/else or switch block, a type guard isn't necessary... Typescript infers the type from the predicate:
Having to declare the type guard also requires us to 'repeat' out the shape of the type which can be quite painful if it isn't a type alias or class... i.e. we have to explicitly write `fb is { type: 'foo' }.
Typescript should be able to infer that the correct type is
{ type: 'foo' }
from the supplied predicate.In such an ideal world, we should be able to simply write:
If Typescript requires us to explicitly opt-in the function as a type-guard, then it should be possible to do so without explicitly repeating the type in the declaration. Maybe something like:
The text was updated successfully, but these errors were encountered: