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

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

Closed
kimamula opened this issue Mar 9, 2016 · 2 comments
Closed

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

kimamula opened this issue Mar 9, 2016 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@kimamula
Copy link
Contributor

kimamula commented Mar 9, 2016

TypeScript Version:

1.8.2

Code

type Click = 'click';

function isClick(event: string): event is Click {
    return event === 'click';
}

function validateClickEvent(event: string): Click {
    if (isClick(event)) {
        return event; // this line raises a compilation error: Type 'string' is not assignable to type '"click"'
    }
    throw new Error('not click');
}

type Creature = {};
type Animal = {} & Creature;

function isAnimal(creature: Creature): creature is Animal {
    return true;
}

function validateAnimalCreature(creature: Creature): Animal {
    if (isAnimal(creature)) {
        return creature; // this line does not raise any compilation error
    }
    throw new Error('not animal');
}

Expected behavior:

Can apply type guard to string literal types.

Actual behavior:

Cannot apply type guard to string literal types.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Mar 9, 2016
@DanielRosenwasser
Copy link
Member

Related to #7224, and a fix exists at #7235.

@sandersn
Copy link
Member

sandersn commented Jun 2, 2016

Fixed by #7235

@sandersn sandersn closed this as completed Jun 2, 2016
@sandersn sandersn added the Fixed A PR has been merged for this issue label Jun 2, 2016
@mhegazy mhegazy removed the Fixed A PR has been merged for this issue label Jun 2, 2016
@mhegazy mhegazy reopened this Jun 2, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jun 6, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants