Skip to content

Incorrect type inference from parameter contraint in conditional types #22221

Closed
@s-ve

Description

@s-ve

TypeScript Version:
2.8.0-dev.201802213

Search Terms:
conditional, inference, constraint

Code

type MustBeString<T extends string> = T;
type EnsureIsString<T> = T extends MustBeString<infer U> ? U : never;

type Test1 = EnsureIsString<'hello'>; //'hello'
type Test2 = EnsureIsString<42>; //42

Expected behavior:
Test2 has type never

Actual behavior:
Test2 has type 42, even though typescript correctly infers that U must be a string in the conditional true branch.

Playground Link:
Related issues:
#21937 #21631

Metadata

Metadata

Assignees

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