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
Function can return wrong values despite type #30603
I think this is an unsoundness that exists to support index access types on this. The assignability rules for Type[K] are defined as:
// A type S is related to a type T[K], where T and K aren't both type variables, if S is related to C,
// where C is the base constraint of T[K]
The base constraint of Type[K] is the type you get when indexing into Type using the base constraint of K which is "a" | "b". So the checker sees that 123 is assignable to Type[K] as:
Would it be too much to restrict the unsoundness to cases where the object type is generic, but the key type is concrete? This example would be the converse and use the sound path.
Activity
jack-williams commentedon Mar 27, 2019
I think this is an unsoundness that exists to support index access types on
this. The assignability rules forType[K]are defined as:The base constraint of
Type[K]is the type you get when indexing intoTypeusing the base constraint ofKwhich is"a" | "b". So the checker sees that123is assignable toType[K]as:123is assignable to123 | "some string"Type[K] ==> Type["a" | "b"] ==> 123 | "some string"The way to prevent the unsoundness would be to write it using two variables, though I'm not sure this actually solves your problem.
EDIT: Just to add the caveat that I think this is what it's doing, and why, but nothing I say is official or guaranteed to be correct.
jack-williams commentedon Mar 27, 2019
See this comment by Anders for (much) more detail.
DanielRosenwasser commentedon Mar 27, 2019
Feels bad - but does that mean we should mark this as a duplicate Anders Hejlsberg (@ahejlsberg)?
jack-williams commentedon Mar 27, 2019
Would it be too much to restrict the unsoundness to cases where the object type is generic, but the key type is concrete? This example would be the converse and use the sound path.
ahejlsberg commentedon Mar 27, 2019
Jack Williams (@jack-williams) I'll give it a try.