-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Infer Type Guard Return Values #5101
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
We should add the overload to the library regardless of this suggestion. @Gaelan feel free to send us PR for updating the library. |
Accepting PRs for the lib.d.ts overload. The more complex portion of inferring type guards based on declare class Foo { bar: number }
declare var blah: Object[];
function isInstance<T>(ctor: new(...args: any[]) => T): (x: any) => x is T {
return <(x: any) => x is T>(x => x instanceof ctor);
}
blah.filter(isInstance(Foo))[0].bar // Property bar does exist :) |
can't quite see how changing |
This is not a general fix, this is only a fix for array.filter. By changing the declaration you get the contextual type for the the function expression at the call. |
The overload for |
#16069 now tracks inferring the type guard automatically. |
If a function returns a type guard, either the result of an
instanceof
or the return value of another type guard function, it could be inferred as being a type guard itself. However, it is simply inferred as aboolean
.This change would allow the following code to work:
The text was updated successfully, but these errors were encountered: