Skip to content

Commit 1530a60

Browse files
authored
Merge pull request #13820 from Microsoft/fixBaseClassCheck
Fix base class check to allow 'object' type
2 parents 207f1aa + 3a0a58d commit 1530a60

File tree

6 files changed

+73
-4
lines changed

6 files changed

+73
-4
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3991,7 +3991,7 @@ namespace ts {
39913991
// A valid base type is any non-generic object type or intersection of non-generic
39923992
// object types.
39933993
function isValidBaseType(type: Type): boolean {
3994-
return type.flags & TypeFlags.Object && !isGenericMappedType(type) ||
3994+
return type.flags & (TypeFlags.Object | TypeFlags.NonPrimitive) && !isGenericMappedType(type) ||
39953995
type.flags & TypeFlags.Intersection && !forEach((<IntersectionType>type).types, t => !isValidBaseType(t));
39963996
}
39973997

@@ -4944,7 +4944,7 @@ namespace ts {
49444944
t.flags & TypeFlags.NumberLike ? globalNumberType :
49454945
t.flags & TypeFlags.BooleanLike ? globalBooleanType :
49464946
t.flags & TypeFlags.ESSymbol ? getGlobalESSymbolType() :
4947-
t.flags & TypeFlags.NonPrimitive ? globalObjectType :
4947+
t.flags & TypeFlags.NonPrimitive ? emptyObjectType :
49484948
t;
49494949
}
49504950

tests/baselines/reference/mixinClassesAnonymous.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ class Thing3 extends Thing2 {
5555
this.print();
5656
}
5757
}
58+
59+
// Repro from #13805
60+
61+
const Timestamped = <CT extends Constructor<object>>(Base: CT) => {
62+
return class extends Base {
63+
timestamp = new Date();
64+
};
65+
}
5866

5967

6068
//// [mixinClassesAnonymous.js]
@@ -138,3 +146,15 @@ var Thing3 = (function (_super) {
138146
};
139147
return Thing3;
140148
}(Thing2));
149+
// Repro from #13805
150+
var Timestamped = function (Base) {
151+
return (function (_super) {
152+
__extends(class_2, _super);
153+
function class_2() {
154+
var _this = _super !== null && _super.apply(this, arguments) || this;
155+
_this.timestamp = new Date();
156+
return _this;
157+
}
158+
return class_2;
159+
}(Base));
160+
};

tests/baselines/reference/mixinClassesAnonymous.symbols

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,22 @@ class Thing3 extends Thing2 {
167167
}
168168
}
169169

170+
// Repro from #13805
171+
172+
const Timestamped = <CT extends Constructor<object>>(Base: CT) => {
173+
>Timestamped : Symbol(Timestamped, Decl(mixinClassesAnonymous.ts, 59, 5))
174+
>CT : Symbol(CT, Decl(mixinClassesAnonymous.ts, 59, 21))
175+
>Constructor : Symbol(Constructor, Decl(mixinClassesAnonymous.ts, 0, 0))
176+
>Base : Symbol(Base, Decl(mixinClassesAnonymous.ts, 59, 53))
177+
>CT : Symbol(CT, Decl(mixinClassesAnonymous.ts, 59, 21))
178+
179+
return class extends Base {
180+
>Base : Symbol(Base, Decl(mixinClassesAnonymous.ts, 59, 53))
181+
182+
timestamp = new Date();
183+
>timestamp : Symbol((Anonymous class).timestamp, Decl(mixinClassesAnonymous.ts, 60, 31))
184+
>Date : Symbol(Date, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
185+
186+
};
187+
}
188+

tests/baselines/reference/mixinClassesAnonymous.types

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,25 @@ class Thing3 extends Thing2 {
198198
}
199199
}
200200

201+
// Repro from #13805
202+
203+
const Timestamped = <CT extends Constructor<object>>(Base: CT) => {
204+
>Timestamped : <CT extends Constructor<object>>(Base: CT) => { new (...args: any[]): (Anonymous class); prototype: <any>.(Anonymous class); } & CT
205+
><CT extends Constructor<object>>(Base: CT) => { return class extends Base { timestamp = new Date(); };} : <CT extends Constructor<object>>(Base: CT) => { new (...args: any[]): (Anonymous class); prototype: <any>.(Anonymous class); } & CT
206+
>CT : CT
207+
>Constructor : Constructor<T>
208+
>Base : CT
209+
>CT : CT
210+
211+
return class extends Base {
212+
>class extends Base { timestamp = new Date(); } : { new (...args: any[]): (Anonymous class); prototype: <any>.(Anonymous class); } & CT
213+
>Base : object
214+
215+
timestamp = new Date();
216+
>timestamp : Date
217+
>new Date() : Date
218+
>Date : DateConstructor
219+
220+
};
221+
}
222+

tests/baselines/reference/nonPrimitiveAssignError.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(5,1): error TS2322: Type 'object' is not assignable to type '{ foo: string; }'.
2-
Property 'foo' is missing in type 'Object'.
2+
Property 'foo' is missing in type '{}'.
33
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(13,1): error TS2322: Type 'number' is not assignable to type 'object'.
44
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(14,1): error TS2322: Type 'true' is not assignable to type 'object'.
55
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(15,1): error TS2322: Type 'string' is not assignable to type 'object'.
@@ -16,7 +16,7 @@ tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(19,1): err
1616
y = a; // expect error
1717
~
1818
!!! error TS2322: Type 'object' is not assignable to type '{ foo: string; }'.
19-
!!! error TS2322: Property 'foo' is missing in type 'Object'.
19+
!!! error TS2322: Property 'foo' is missing in type '{}'.
2020
a = x;
2121
a = y;
2222

tests/cases/conformance/classes/mixinClassesAnonymous.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ class Thing3 extends Thing2 {
5454
this.print();
5555
}
5656
}
57+
58+
// Repro from #13805
59+
60+
const Timestamped = <CT extends Constructor<object>>(Base: CT) => {
61+
return class extends Base {
62+
timestamp = new Date();
63+
};
64+
}

0 commit comments

Comments
 (0)