Repository navigation
Fails to infer type parameters from where return value is assigned #21275
Copy link
Copy link
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: 2.7.0-dev.20180117
Search Terms:
generic, return, infer, inference, type parameter
Code
type Path<T, V> = Array<string>
function path<T, A extends keyof T>(key: A): Path<T, T[A]>
function path<T>(path: string|Array<string>): Path<T, any> {
if (typeof path === 'string') return [path] as Path<T, any>
else return path as Path<T, any>
}
function field<T, V>(path: Path<T, V>) {
return {path}
}
type User = {name: string}
// Errors
field<User, string>(path('name'))
// Works
field<User, string>(path<User, 'name'>('name'))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:
- The compiler detects that it's not possible to infer
Path::Tand therefor alsoPath::Afrom the arguments - The compiler realizes that the return value is
Path<T, T[A]>and is assigned toPath<User, string>and can therefor infer thatT = User - As the compiler now knows T it now knows that A is
A extends keyof User - It can now infer that
A = 'name'from the argument - It now knows that
T[A]isUser['name']which isstring - Finally, as the return value now is fully resolved as
Path<User, string>which is assigned to an argument of typePath<User, string>there should be no build errors
Actual behavior:
Error: Argument of type '"name"' is not assignable to parameter of type 'never'..
Presumably Path::T is inferred to be {} and Path::A naturally then becomes never
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Activity
jcalz commentedon Jan 18, 2018
Related to #11152, maybe?
RyanCavanaugh commentedon Jan 18, 2018
There may be more going on here, but the unconsumed type parameters in
Pathare definitely problematic. Intersecting some optional properties of typeTandVfix the issue.mhegazy commentedon Jan 19, 2018
I believe this is just a duplicate of #11152.
Pajn commentedon Jan 19, 2018
Seems to be a duplicate of 11152. Joe Calzaretta (@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.
Ryan Cavanaugh (@RyanCavanaugh) That was an initial suspicion of mine, but I did try something like
& {__type_tag?: [T, A]}without any difference.jcalz commentedon Jan 19, 2018
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.