Skip to content

Infer Type Guard Return Values #5101

@Gaelan

Description

@Gaelan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions