Skip to content

Conditional types don't work with passed never type #23022

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

Closed
falsandtru opened this issue Mar 30, 2018 · 5 comments
Closed

Conditional types don't work with passed never type #23022

falsandtru opened this issue Mar 30, 2018 · 5 comments
Labels
Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@falsandtru
Copy link
Contributor

cc @ahejlsberg

TypeScript Version: 2.7.0-dev.20180330

Search Terms:

Code

type C<T, U> = T extends U ? true : false;
type A = never extends void ? true : false;
type B = C<never, void>;

Expected behavior:

A and B are true.

Actual behavior:

A is true, B is never.

Playground Link:

Related Issues:

@jack-williams
Copy link
Collaborator

I believe this is working as intended: C is distributive but A is not. C is being distributed zero times as never is empty.

@falsandtru
Copy link
Contributor Author

A is inlined B. Inlining must not make any different behavior.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 30, 2018

A is inlined B. Inlining must not make any different behavior.

not really. a type is distributed if it has a naked type parameter in the condition close. so

type inline = number | string extends string ? true : false; // false

type deferred<T> = T extends string ? true : false;
type d = deferred<number | string>;  // boolean

The rational here that using a type parameter in the condition position is like a function on the input, and is expected to distribute, where as an actual union type is mean to be used as is.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 30, 2018
@falsandtru
Copy link
Contributor Author

I understood, this is not inlining. A passed union type is not determined as its exact union type yet. It also can be a subtype.

@mhegazy mhegazy added the Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design label Apr 2, 2018
@falsandtru
Copy link
Contributor Author

A workaround:

type C<T, U> = [T] extends [never] ? 1 : T extends U ? 1 : 0;
type a = C<never, void>; // 1
type b = C<never, never>; // 1
type c = C<boolean, true>; // 0 | 1

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants