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 constraint not playing well with new 'object' type. #15920
In the code below, I am am uncertain if this is a bug or a misunderstanding on my part to do with the new, more restrictive, 'object' type.
Code
// The following produces a compiler error:// Type 'A & B' does not satisfy the constraint 'Base'.// Type 'A & B' is not assignable to type 'object'.// (type parameter) B in foo<A extends Base, B extends Base>(): Foo<Base, A & B>//typeBase=boolean|object;typeFoo<DOM,CODOMextendsDOM>=(dom: DOM)=>CODOM;functionfoo<AextendsBase,BextendsBase>(): Foo<Base,A&B>// <= error at A & B{return<any>function(x: Base){returnx;};}// Removing the constraint on CODOM in type Foo2 and// all is well.//typeBase2=boolean|object;typeFoo2<DOM,CODOM>=(dom: DOM)=>CODOM;// <= "fixed" by removing extends DOMfunctionfoo2<AextendsBase2,BextendsBase2>(): Foo2<Base2,A&B>{return<any>function(x: Base2){returnx;};}// All can also be made well by changing object to Object in// type Base3.//typeBase3=boolean|Object;// <= also "fixed" by resorting to 'Object' instead of 'object'typeFoo3<DOM,CODOMextendsDOM>=(dom: DOM)=>CODOM;functionfoo3<AextendsBase3,BextendsBase3>(): Foo3<Base3,A&B>{return<any>function(x: Base2){returnx;};}
The issue relates to how we break down union and intersection types in type relationship checking. We always first break down union types, i.e. we check that A & B is assignable to object or boolean. Neither of those are true (because, for example, A could be a boolean which isn't assignable to object). Effectively, when checking the constraints, we end up with union types sitting below an intersection type, which produces wrong results. In order to solve this we would have to "lift" the constraints up and distribute the & operator over any | operators in the constraints. It's doable, but not trivial.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
ahejlsberg commentedon May 17, 2017
A simpler repro:
The issue relates to how we break down union and intersection types in type relationship checking. We always first break down union types, i.e. we check that
A & Bis assignable toobjectorboolean. Neither of those are true (because, for example,Acould be abooleanwhich isn't assignable toobject). Effectively, when checking the constraints, we end up with union types sitting below an intersection type, which produces wrong results. In order to solve this we would have to "lift" the constraints up and distribute the&operator over any|operators in the constraints. It's doable, but not trivial.ahejlsberg commentedon May 17, 2017
See #11717 for context on union/intersection normalization.
typescript-bot commentedon Aug 1, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.