Open xieyuheng opened 3 years ago
There are two problems here, both of which might be bugs but Iโm not confident that either is. Itโs easiest to demonstrate minimal versions of each.
The first is that head
and/or tail
is acting as an inference source for T
in cons
. You might expect that neither would be, since each matches the Var
constituent of Logical
, we shouldnโt have to try to infer to the { [P in keyof T]: Logical<T[P]> }
constituent at all. If this were the case, the only remaining inference target would be the return type Logical<List<T>>
which would match up with the contextual type Logical<List<string>>
from test
. However, we clearly are trying to infer from head
and/or tail
to { [P in keyof T]: Logical<T[P]> }
(and we succeed at that), so the inference is fixed before the weaker return type inference can play a role. You can see a simplified example of this happening here.
The second problem is why the error messages are so awful: we are forming reverse mapped types for the properties of primitives. A generic mapped type applied to a primitive returns just the primitive, so you might expect a reverse mapped type inferred from a primitive to just be the primitive as well. This is a bit hard to reason about, as it's hard to come up with a meaningful mapped type that a primitive would be assignable to in the first place, but there is clearly an asymmetry. You can see it in action here.
@andrewbranch Thanks for your reply, and your simplified examples :)
name: Bug about: Create a report to help us improve TypeScript title: Typescript fail to unify type variables labels: '' assignees: ''
Bug Report
๐ Search Terms
unify type variables
,unification
,type inference
.๐ Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about
Generics
.โฏ Playground Link
Playground link with relevant code
๐ป Code
๐ Actual behavior
When I do not write the annotation for
cons
, an error occured:๐ Expected behavior
I expect type checker inference the type annotation for me, so I can write:
Instead of: