Skip to content
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

Type 'T' is not assignable to type 'T extends Sub ? Sub : T'. #48746

Closed
taralx opened this issue Apr 18, 2022 · 6 comments
Closed

Type 'T' is not assignable to type 'T extends Sub ? Sub : T'. #48746

taralx opened this issue Apr 18, 2022 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@taralx
Copy link

taralx commented Apr 18, 2022

Bug Report

πŸ”Ž Search Terms

variance, conditional, subtype, limit

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about variance and conditionals.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface Base {
    base: unknown;
}

interface Sub extends Base {
    sub: unknown;
}

function transform<T extends Base>(t: T): T extends Sub ? Sub : T {
    return t;
}

πŸ™ Actual behavior

"Type 'T' is not assignable to type 'T extends Sub ? Sub : T'."

πŸ™‚ Expected behavior

No errors. Either T extends Sub, and so is assignable to it, or it doesn't, and so is assignable to itself.

@MartinJohns
Copy link
Contributor

See #48243, #47633 and probably many others.

Resolving of conditional types involving unbound generic type arguments is deferred. The compiler doesn't know what type T extends Sub ? Sub : T resolves to, because it doesn't know what type T is.

@taralx
Copy link
Author

taralx commented Apr 18, 2022

Any recommendations on how to express this differently that doesn't run into this problem? Perhaps there is a way to express the join/meet operation without conditionals.

@taralx
Copy link
Author

taralx commented Apr 18, 2022

FWIW I tried using [T] extends [Sub] to avoid distributive behavior but it doesn't help.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 18, 2022
@whzx5byb
Copy link

whzx5byb commented Apr 18, 2022

Any recommendations on how to express this differently that doesn't run into this problem? Perhaps there is a way to express the join/meet operation without conditionals.

interface Base {
    base: unknown;
}

interface Sub extends Base {
    sub: unknown;
}

function transform<T extends Base>(t: T extends Sub ? T & Sub : T): T extends Sub ? Sub : T {
    return t;
}

var t = transform({base: 0, foo: 0})
// ^? var t: { base: number, foo: number }
var sub = transform({base: 0, sub: 0, foo: 0})
//  ^? var sub: Sub

I don't know why T extends Sub ? T & Sub : T is assignable to T extends Sub ? Sub : T since it doesn't meet the conditions mentioned in #46429.
Seems like a black magic 😎

@taralx
Copy link
Author

taralx commented Apr 18, 2022

Maybe syntax-driven? Would make sense.

@typescript-bot
Copy link
Collaborator

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

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

5 participants