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
The type A & B has a property P if A has a property P or B has a property P. If A has a property P of type X and B has a property P of type Y, then A & B has a property P of type X & Y.
Regarding order and errors, a key feature of the & type operator is that it can be applied to type parameters. This deeply affects the semantics of the operator, particularly as it relates to ordering and error reporting, as I explain here.
A bit more regarding ordering: With the current rules we can assume A & B is a subtype of (and assignable to) both A and B, even when A and B are type parameters. With "last one wins" we could only assume A & B is a subtype of B (because B might override members of A). That would lead to a number of surprises and inconsistencies.
Like Anders Hejlsberg (@ahejlsberg) explained earlier intersection tupes do not have a concept of a conflict; if the two types have a property with the same name, the intersection of the will have a property with the intersection of the property type in both. The rational is we have no way to resolve these conflicts correctelly, nor s good place to show the error.
I think what you are looking for is traits or mixin support for classes; if and when this is supported, we should be able to enforce a resolution policy and flag conflicts. We shied away from these as the Emmasctipt committee showed interest in pursuing it and we do not want to conflict with the standard.
Jon (@jbondc) Type instantiation is aggressively deferred (i.e. it only happens if and when it needs to) and it is not possible to consistently report errors during instantiation (because it may never happen). We also can't eagerly instantiate types because we quickly end up in infinite instatation chains when types reference themselves.
We really can not do that. by the time you discover the conflict, you are way deep in instantiating a type, and there is not much meaningful information you can give to the user. this is one of the main reasons intersection types work the way they do.
Activity
mhegazy commentedon Sep 15, 2015
numis an enum type, not the enum value, so on bothenumClassandclassEnumahas only one declaration, which is a function.i think what you mean is:
this is described in #3622:
ahejlsberg commentedon Sep 15, 2015
Regarding order and errors, a key feature of the
&type operator is that it can be applied to type parameters. This deeply affects the semantics of the operator, particularly as it relates to ordering and error reporting, as I explain here.A bit more regarding ordering: With the current rules we can assume
A & Bis a subtype of (and assignable to) bothAandB, even whenAandBare type parameters. With "last one wins" we could only assumeA & Bis a subtype ofB(becauseBmight override members ofA). That would lead to a number of surprises and inconsistencies.mhegazy commentedon Sep 16, 2015
Like Anders Hejlsberg (@ahejlsberg) explained earlier intersection tupes do not have a concept of a conflict; if the two types have a property with the same name, the intersection of the will have a property with the intersection of the property type in both. The rational is we have no way to resolve these conflicts correctelly, nor s good place to show the error.
I think what you are looking for is traits or mixin support for classes; if and when this is supported, we should be able to enforce a resolution policy and flag conflicts. We shied away from these as the Emmasctipt committee showed interest in pursuing it and we do not want to conflict with the standard.
ahejlsberg commentedon Sep 16, 2015
Jon (@jbondc) Type instantiation is aggressively deferred (i.e. it only happens if and when it needs to) and it is not possible to consistently report errors during instantiation (because it may never happen). We also can't eagerly instantiate types because we quickly end up in infinite instatation chains when types reference themselves.
mhegazy commentedon Sep 16, 2015
We really can not do that. by the time you discover the conflict, you are way deep in instantiating a type, and there is not much meaningful information you can give to the user. this is one of the main reasons intersection types work the way they do.