-
Notifications
You must be signed in to change notification settings - Fork 12.8k
In function argument, applying a generic type that extends an object type to a conditional type operator doesn't specialise #56011
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
Comments
It's a design limitation. There are numerous issues about this. Resolving of conditional types involving unbound generic type arguments are deferred until the type is known. In your example the type of Duplicate of #53455 and others. |
Thank you for the swift response! I see. And by "design limitation" you mean that there is no intention of trying to support this, even if it is theoretically possible? I want to add that it seems to me that this case is more resolvable than #53455. In that issue, it's theoretically impossible to resolve the conditional (in isolation within the body of the function) because the unbound generic type Contrarily, in the example of this issue, the unbound generic type In other words, and with naivety of the hairy details of the TypeScript type checker, I don't see why conditionals that can be resolved at type checking time necessarily need to be deferred. |
There are actually cases where the compiler can resolve conditional types over generics if it can prove they always resolve a particular way. However, in your given example, the conditional type is distributive (i.e. it maps over unions) - which to my knowledge is always deferred because the compiler has no way of knowing how many times the conditional has to be applied. It might even be 0 times, if the input type happens to be |
btw, your Playground and posted code differ. The issue form tells you not to do this
|
Sorry, I realized after posting the first link that I could simplify the example. After doing so, I forgot to refresh the link to the playground. Will do so now. |
In my example the first application of the conditional may be statically determined: since |
Anyway, you know that |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Thanks for explaining this, @fatcerberus
Of course, I can't argue with this not knowing the design of the compiler. It's just still surprising to me that such a mechanism should be problematic to implement. It seems to me a matter of implementing a pattern match saying that a type internally represented as:
may immediately be reduced internally to
But anyway, issue closed, I'll just have to work around it, and please don't feel you need to respond to this. |
@johanrosenkilde FWIW, your Footnotes
|
🔎 Search Terms
"inference conditional", "automatic inference", "generic function argument", "object type"
🕗 Version & Regression Information
⏯ Playground Link
Link
💻 Code
🙁 Actual behavior
In
unexpectedlyDoesntWork
,input
seems to be type inferred asId<Vals>
i.e.Vals
, and soinput.a
is inferred asVals
. Hence thesatisfies
clause fails. This is not the most precise interpretation, sinceB
is known to extendFamily
and hence be of object type with a membera
, and soId<B>
should specialise to{ [K in keyof B]: Id<B[K]> }
.🙂 Expected behavior
I expect
input.a
to be inferred as typeId<B["a"]>
. This occurs in theworksFine
function, where the only difference is that the function value has applied the non-conditional type operatorIdObj
toB
.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: