Skip to content

Conditional type does not work when generic #58765

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
ehmicky opened this issue Jun 3, 2024 · 2 comments
Closed

Conditional type does not work when generic #58765

ehmicky opened this issue Jun 3, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@ehmicky
Copy link

ehmicky commented Jun 3, 2024

πŸ”Ž Search Terms

intersection, empty, generic

πŸ•— Version & Regression Information

This is the behavior in every version I tried (from 3.3.3 to 5.5.0-beta), and I reviewed the FAQ for entries about intersections and conditional types.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.0-beta#code/C4TwDgpgBAggznCAnY84EsDmA7AhgIwBsIAeAVQBooAVKCAD2AmwBM4oyA+KAXhoFgAUENCRYJWnwCu2ANbYA9gHds3PgG8wSBWABcUAAx1GzNjSgB+KMCRTo+gGa5CiAL4BuEeGgwATL1gEZFQELDwiUhgqGBIDTk53KAB6JKgAMVx0FyEvMQAhCQCZeWVVAM1tPUNjJlZ2WisbOyhHZzcoADIodQ9c6Dz-PjRgtDCCYhI8qgK4hOTUgGUpAGNliAg2HMFRaABhQuk5RRU1aoZaswbrW3soJxcIT23vKF3BwMQUUZxx0l2qfazRIpKBLVbrNhAA

πŸ’» Code

type AssertAssignable<U, T extends U> = T

type A<T = unknown> = {prop: 0 extends T ? true : false};
type A2 = AssertAssignable<A, A<0>>; // Fails

type B<T = unknown> = {prop: 0 extends T ? true : false} & {};
type B2 = AssertAssignable<B, B<0>>; // Succeeds

type C<T = unknown> = 0 extends T ? true : false;
type C2 = AssertAssignable<C, C<0>>; // Succeeds

πŸ™ Actual behavior

Type A2 fails with:

Type 'A<0>' does not satisfy the constraint 'A<unknown>'.
  Type 'unknown' is not assignable to type '0'.

πŸ™‚ Expected behavior

A<0> should be assignable to A. I.e. A2 should not fail.

Additional information about the issue

B2 does not fail, i.e. adding & {} seems to strangely work around this problem.

C2 does not fail, i.e. this problem seems to only occur inside object properties.

@RyanCavanaugh
Copy link
Member

Duplicate #44945

It's looking increasingly likely the only good fix here is going to be introducing an unreliable (name???) companion to the in and out variance markers and let users specify that

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 6, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants