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

Infer Type Guard Return Values #5101

Closed
Gaelan opened this issue Oct 4, 2015 · 6 comments
Closed

Infer Type Guard Return Values #5101

Gaelan opened this issue Oct 4, 2015 · 6 comments
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@Gaelan
Copy link

Gaelan commented Oct 4, 2015

If a function returns a type guard, either the result of an instanceofor the return value of another type guard function, it could be inferred as being a type guard itself. However, it is simply inferred as a boolean.

This change would allow the following code to work:

// The interface below should probably be merged into lib.d.ts and friends.
interface Array<T> {
    filter<O extends T>(callbackfn: (value: T, index: number, array: T[]) => value is O, thisArg?: any): O[];
}

declare class Foo { bar: number }

declare var blah: Object[];

blah.filter(object => object instanceof Foo)[0].bar // Property bar does not exist on type object
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Oct 5, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Oct 5, 2015

We should add the overload to the library regardless of this suggestion. @Gaelan feel free to send us PR for updating the library.

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript and removed In Discussion Not yet reached consensus labels Oct 5, 2015
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Oct 5, 2015
@RyanCavanaugh
Copy link
Member

Accepting PRs for the lib.d.ts overload.

The more complex portion of inferring type guards based on return statements was declined based on complexity. It's fun and easy to write a curried version of this that works today (with the additional overload for filter):

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 :)

@zpdDG4gta8XKpMCd
Copy link

can't quite see how changing libs.d.ts is able to fix this problem, mind sharing idea?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 5, 2017

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.

@mhegazy
Copy link
Contributor

mhegazy commented May 2, 2018

The overload for Array.filter should be in already. closing.

@mhegazy
Copy link
Contributor

mhegazy commented May 2, 2018

#16069 now tracks inferring the type guard automatically.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants