You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Generic interface with a union type involving a generic function causes problems with unknown #39843
Expected behavior: Container<string> may be added to Array<Container<unknown>>
Actual behavior:
Argument of type 'Container<string>' is not assignable to parameter of type 'Container<unknown>'.
Type 'unknown' is not assignable to type 'string'.(2345)
This is caused by a variance measurements which marks Container invariant in State because a function of type (x: A) => A is invariant in A. The measurement means that Container<A> is assignable to Container<B> if A is identical to B, which string and unknown are not.
The reason the union triggers the error is because there is special logic to treat callbacks bivariantly, but the addition of the union to the argument of update no longer marks the argument as a callback, so it gets related strictly.
I believe this is working as intended, thought it might be slightly tricky to unpick.
Assuming that you are happy with some (technical) unsoundness, which may be ok if you only call update with the value stored in the container, then one solution is the bivariance hack:
Activity
jack-williams commentedon Aug 2, 2020
This is caused by a variance measurements which marks
Containerinvariant inStatebecause a function of type(x: A) => Ais invariant in A. The measurement means thatContainer<A>is assignable toContainer<B>ifAis identical toB, whichstringandunknownare not.The reason the union triggers the error is because there is special logic to treat callbacks bivariantly, but the addition of the union to the argument of
updateno longer marks the argument as a callback, so it gets related strictly.I believe this is working as intended, thought it might be slightly tricky to unpick.
Assuming that you are happy with some (technical) unsoundness, which may be ok if you only call
updatewith thevaluestored in the container, then one solution is the bivariance hack:RyanCavanaugh commentedon Aug 20, 2020
Jack Williams (@jack-williams) 's analysis is correct as usual
typescript-bot commentedon Aug 23, 2020
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.