Skip to content

Type guard function does not work for string literal types #7441

Description

TypeScript Version:

1.8.2

Code

typeClick='click';functionisClick(event: string): event is Click{returnevent==='click';}functionvalidateClickEvent(event: string): Click{if(isClick(event)){returnevent;// this line raises a compilation error: Type 'string' is not assignable to type '"click"'}thrownewError('not click');}typeCreature={};typeAnimal={}&Creature;functionisAnimal(creature: Creature): creature is Animal{returntrue;}functionvalidateAnimalCreature(creature: Creature): Animal{if(isAnimal(creature)){returncreature;// this line does not raise any compilation error}thrownewError('not animal');}

Expected behavior:

Can apply type guard to string literal types.

Actual behavior:

Cannot apply type guard to string literal types.

Metadata

Metadata

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