Skip to content

Type narrowing of string literal unions #9314

Closed
@gcnew

Description

@gcnew

TypeScript Version:

nightly (1.9.0-dev.20160217) + strictNullChecks

Code

type Easing = 'ease-in' | 'ease-out' | 'ease-in-out';

function getEasingFunction(easing: Easing): (val: number) => number {
    switch (easing) {
        case 'ease-in':     return t => t * t;
        case 'ease-out':    return t => t * (2-t);
        case 'ease-in-out': return t => t<0.5 ? (2*t*t) : -1+(4-2*t)*t;
    }
    // `easing` should be of type `never` here
    // however the actual type is `'ease-in' | 'ease-out' | 'ease-in-out'`
}

Expected behavior:
After an exhaustive switch the switched variable should be of type never as per #9163. Conversely, if the switch is not exhaustive and the return type does not accomodate for undefined, an error should be issued.

Actual behavior:
String literal union types are not narrowed thus an error is always issued.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions