Skip to content

Type parameter constrained to union cannot be exhaustively narrowed to 'never' #13215

Open
@Strate

Description

TypeScript Version: 2.1.1 (http://www.typescriptlang.org/play/index.html)

Code

type Type = "a" | "b"

function isA(a: any): a is "a" {
    return a === "a"
}

function isB(a: any): a is "b" {
    return a === "b"
}

function assertNever(arg: never) {
    throw new Error("This should never be called")
}

function handleAction<T extends Type>(type: T) {
    if (isA(type)) {
        return type
    } else if (isB(type)) {
        return type
    } else {
        assertNever(type)
    }
}

Expected behavior:
No compilation error

Actual behavior:
Argument of type 'T' is not assignable to type 'never'

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions