Skip to content

Conditional type using never type does not work in generic functions #54806

@dummdidumm

Description

@dummdidumm

Bug Report

When using a conditional type that checks for the never type inside a function with a generic parameter that check is not correctly executed and the type is not narrowed accordingly, even if the generic has a constraint that makes clear that the generic type can't be never (though it should probably also work without a constraint).

🔎 Search Terms

generic function narrow condition type never

Possibly related to #49852 / #53455

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about generic functions and never type

⏯ Playground Link

Playground link with relevant code

💻 Code

type IsNever<Type> = [Type] extends [never] ? undefined : string;
type IsBoolean<Type> = Type extends boolean ? string : undefined;

declare function neverFn<T>(t: T): IsNever<T>;
declare function booleanFn<T>(t: T): IsBoolean<T>;

function genericFn<T extends boolean>(t: T) {
	const booleanResult = booleanFn(t);
	booleanResult.toString() // works: booleanResult is correctly narrowed to type string

	const neverResultFromLiteral = neverFn(true);
	neverResultFromLiteral.toString(); // works: neverResultFromLiteral is correctly narrowed to type string

	const neverResultFromGeneric = neverFn(t);
	neverResultFromGeneric.toString(); // error: neverResultFromGeneric is not correctly narrowed to type string
}

🙁 Actual behavior

neverResultFromGeneric is not correctly narrowed to type string

🙂 Expected behavior

neverResultFromGeneric is correctly narrowed to type string

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions