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
Declaration order dependence when inferring to a union #45603
There's some weird wonkyness in the Playground. Depending on whether you hover over the first or second call function call you get a different tooltip for the first call:
changed the title [-]Cannot infer more than one generic type in a function that receives an object and another function as parameters[/-][+]Declaration order dependence when inferring to a union[/+]on Aug 27, 2021
The thing that makes this particularly bad is that which call errors is dependent on whether actionA or actionB is declared first (and therefore the internal ordering of the union typeof actionA | typeof actionB).
That looks great! However I simplified the problem a bit to a better understanding. So this workaround does not work in my case.
It has two interfaces sharing the same generics. Specifically PayloadActionCreator and PayloadAction. These are part of typesafe-actions lib
typeA={kind: 'a'};typeB={kind: 'b'};declareconsta: A;declareconstb: B;declarefunctionfab(arg: A|B): void;declarefunctionfoo<T>(x: {kind: T},f: (arg: {kind: T})=>void): void;foo(a,fab);// Okfoo(b,fab);// Error, but shouldn't be
The issue here is that we make two contra-variant inferences for T (namely 'a' and 'b') and then pick one of the two based on the sort order of type identities--which so happens to be dictated by declaration order. We have logic to prefer a co-variant inference if it is a subtype of the contra-variant inference, but that happens after we've (randomly) picked one of the contra-variant inferences.
What we really should do is check if the co-variant inference is a subtype of any of the contra-variant inferences, and, if so, go with the co-variant inference. The random picking should really be a last resort since we know it's going to lead to an error downstream (it's still preferable to intersecting the contra-variant inferences because that'll produce a never when the inferences are disjoint).
Activity
MartinJohns commentedon Aug 27, 2021
There's some weird wonkyness in the Playground. Depending on whether you hover over the first or second
callfunction call you get a different tooltip for the first call:andrewbranch commentedon Aug 27, 2021
Perhaps a better workaround: https://www.typescriptlang.org/play?ts=4.5.0-dev.20210826#code/JYOwLgpgTgZghgYwgAgIILMA9iAPAFQDk4BbFCAD0hABMBnZOsKUAcwBp8AFOATwBsscGgD5kAbwBQyGchCkIALmREF7abIAOfQcOXcdQmpIC+kyQhxNkiTDgBCyALwTk2gUeXMArihM2GdDs8AHJUAGF8AEkAeUIAfXsQ9mQAIywsfgg4EBELKzAbDGwQVGdXd10aZRCc3gBaJhYQVhDkfzhA4pxcMMjYhNRkxmY2PMkYbxBukGQEOH5+AiCS5EpqejQZ3Ca2FKmAaxAsAHdckQAKDVsS-RWcdRkYEGULm5w7mYBKJzE69S+Eg0zze31M5ksIGsmmaYAAYrMXKDgl5eJoIFgYEVgmUAD7IMBojFY94geyA35zKyZCAAOkErGRJS+EIW-CZOFQKRhoHhIBZ80WHLJ3NhCJZQA
[-]Cannot infer more than one generic type in a function that receives an object and another function as parameters[/-][+]Declaration order dependence when inferring to a union[/+]andrewbranch commentedon Aug 27, 2021
The thing that makes this particularly bad is that which call errors is dependent on whether
actionAoractionBis declared first (and therefore the internal ordering of the uniontypeof actionA | typeof actionB).afonsof commentedon Aug 31, 2021
That looks great! However I simplified the problem a bit to a better understanding. So this workaround does not work in my case.
It has two interfaces sharing the same generics. Specifically
PayloadActionCreatorandPayloadAction. These are part oftypesafe-actionslibTake a look at this new sample:
https://www.typescriptlang.org/play?ts=4.5.0-dev.20210826#code/JYOwLgpgTgZghgYwgAgIILMA9iAPAFQDk4BbFCAD0hABMBnZOsKUAcwBp8AFOATwBsscGgD5kAbwBQyGchCkIALmREF7abIAOfQcOXcdQmpIC+kyaEixEKdJhwB1LFADW0AsTLJK1eo2ZsnDwCRmJSsnIK+p4Q6hHaIXoqwbrGZpIIOEzIiPYgAELIALwSyAmpyswArigmOQx22CBOru4A5KgAwvgAkgDyhAD6+W3syABGWFj8EHAgIhlZYDkYTajFpeVGym1zvAC0TCwgrG3IdXANq47OblC4Hd39Q6ij-sesC5IwVSDXIMgEHB+PwCPheJpyFQILQGEdAslDMIRAAKDS5JrKRo4MEQ2KIxKiOLIGAgZQojE4LH-Fp3XGQoJI0QASiKYj26mZEg0pIp-2ZpnMmRA2U0xzAADEASU+XlKnisDAVnl1gAfZBgBVKykFLlswFZaYQAB0glYsqaAoywP4FpwqDGYssUoFQJBdoKjvFLskQA
ahejlsberg commentedon Oct 15, 2021
Simpler repro:
The issue here is that we make two contra-variant inferences for
T(namely'a'and'b') and then pick one of the two based on the sort order of type identities--which so happens to be dictated by declaration order. We have logic to prefer a co-variant inference if it is a subtype of the contra-variant inference, but that happens after we've (randomly) picked one of the contra-variant inferences.What we really should do is check if the co-variant inference is a subtype of any of the contra-variant inferences, and, if so, go with the co-variant inference. The random picking should really be a last resort since we know it's going to lead to an error downstream (it's still preferable to intersecting the contra-variant inferences because that'll produce a
neverwhen the inferences are disjoint).