-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Fails to infer type parameters from where return value is assigned #21275
Comments
Related to #11152, maybe? |
There may be more going on here, but the unconsumed type parameters in |
I believe this is just a duplicate of #11152. |
Seems to be a duplicate of 11152. @jcalz May I ask what searchterm you used to find that? I expected something would be there but no matter what I tried I could not find it. @RyanCavanaugh That was an initial suspicion of mine, but I did try something like |
I think I just searched for "return" and "inference", which are in your list of terms. My advantage was the vague recollection that I had seen such an issue before. |
TypeScript Version: 2.7.0-dev.20180117
Search Terms:
generic, return, infer, inference, type parameter
Code
Expected behavior:
path should have all type information it needs by the fully typed field call and therefor build without explicit type arguments.
If I can freely imagine the steps the compiler would take:
Path::T
and therefor alsoPath::A
from the argumentsPath<T, T[A]>
and is assigned toPath<User, string>
and can therefor infer thatT = User
A extends keyof User
A = 'name'
from the argumentT[A]
isUser['name']
which isstring
Path<User, string>
which is assigned to an argument of typePath<User, string>
there should be no build errorsActual behavior:
Error:
Argument of type '"name"' is not assignable to parameter of type 'never'.
.Presumably
Path::T
is inferred to be{}
andPath::A
naturally then becomesnever
Playground Link: https://www.typescriptlang.org/play/#src=type%20Path%3CT%2C%20V%3E%20%3D%20Array%3Cstring%3E%0D%0A%0D%0Afunction%20path%3CT%2C%20A%20extends%20keyof%20T%3E(key%3A%20A)%3A%20Path%3CT%2C%20T%5BA%5D%3E%0D%0Afunction%20path%3CT%3E(path%3A%20string%7CArray%3Cstring%3E)%3A%20Path%3CT%2C%20any%3E%20%7B%0D%0A%20%20if%20(typeof%20path%20%3D%3D%3D%20'string')%20return%20%5Bpath%5D%20as%20Path%3CT%2C%20any%3E%0D%0A%20%20else%20return%20path%20as%20Path%3CT%2C%20any%3E%0D%0A%7D%0D%0A%0D%0Afunction%20field%3CT%2C%20V%3E(%0D%0A%20%20path%3A%20Path%3CT%2C%20V%3E%0D%0A)%20%7B%0D%0A%20%20return%20%7Bpath%7D%0D%0A%7D%0D%0A%0D%0Atype%20User%20%3D%20%7Bid%3A%20string%2C%20name%3A%20string%2C%20birthdate%3F%3A%20Date%7D%0D%0A%0D%0A%2F%2F%20Errors%0D%0Afield%3CUser%2C%20string%3E(path('name'))%0D%0A%0D%0A%2F%2F%20Works%0D%0Afield%3CUser%2C%20string%3E(path%3CUser%2C%20'name'%3E('name'))
Related Issues:
There are many issues related to inference or generics but I can't find any related that I imagines involves the same resolution logic.
The text was updated successfully, but these errors were encountered: