Skip to content

Generic type erased after type guard with strictNullChecks #10811

@jgoz

Description

@jgoz

TypeScript Version: 2.0.2

Code

interface Pet {
    name: string;
}

function isPet(pet: any): pet is Pet {
    return typeof pet.name === "string";
}

export function speak<TPet extends Pet>(pet: TPet, voice: (pet: TPet) => string): string {
    if (!isPet(pet)) {
        throw new Error("Expected \"pet\" to be a Pet");
    }

    return voice(pet); // line 14
}

Expected behavior:
Module compiles without error when --strictNullChecks is enabled OR disabled.

Actual behavior:

  • Module compiles without error when --strictNullChecks is disabled.

  • The following error is produced when --strictNullChecks is enabled

    bug.ts(14,18): error TS2345: Argument of type 'Pet' is not assignable to parameter of type 'TPet'.
    

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions