Skip to content

User-defined type guards don't work for all types #4432

Closed
@sophiajt

Description

@sophiajt

If the user defines a type guard for a boolean, as in this example:

function isBool(a: any): a is boolean {
    return (typeof a === "boolean");
}

function bob() {
    var z;

    if (isBool(z)) {
        z; // type of z is still 'any'
    }
}

...then z is not properly refined to 'boolean'. It stays 'any'. I think the expected behavior here is for the type guard to refine to boolean inside of the if check.

Metadata

Metadata

Labels

DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions