Skip to content

Commit 746c45c

Browse files
authored
Merge pull request #14250 from Microsoft/fix-crash-in-isConstructorType
isConstructorType checks base constraint for undefined
2 parents 31c4ad1 + dbaf1f6 commit 746c45c

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3952,7 +3952,7 @@ namespace ts {
39523952
}
39533953
if (type.flags & TypeFlags.TypeVariable) {
39543954
const constraint = getBaseConstraintOfType(<TypeVariable>type);
3955-
return isValidBaseType(constraint) && isMixinConstructorType(constraint);
3955+
return constraint && isValidBaseType(constraint) && isMixinConstructorType(constraint);
39563956
}
39573957
return false;
39583958
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
tests/cases/compiler/baseConstraintOfDecorator.ts(2,5): error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'.
2+
tests/cases/compiler/baseConstraintOfDecorator.ts(2,40): error TS2507: Type 'TFunction' is not a constructor function type.
3+
4+
5+
==== tests/cases/compiler/baseConstraintOfDecorator.ts (2 errors) ====
6+
export function classExtender<TFunction>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
7+
return class decoratorFunc extends superClass {
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
~~~~~~~~~~
10+
!!! error TS2507: Type 'TFunction' is not a constructor function type.
11+
constructor(...args: any[]) {
12+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13+
super(...args);
14+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
15+
_instanceModifier(this, args);
16+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17+
}
18+
~~~~~~~~~
19+
};
20+
~~~~~~
21+
!!! error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'.
22+
}
23+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [baseConstraintOfDecorator.ts]
2+
export function classExtender<TFunction>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
3+
return class decoratorFunc extends superClass {
4+
constructor(...args: any[]) {
5+
super(...args);
6+
_instanceModifier(this, args);
7+
}
8+
};
9+
}
10+
11+
12+
//// [baseConstraintOfDecorator.js]
13+
"use strict";
14+
var __extends = (this && this.__extends) || (function () {
15+
var extendStatics = Object.setPrototypeOf ||
16+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
17+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
18+
return function (d, b) {
19+
extendStatics(d, b);
20+
function __() { this.constructor = d; }
21+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
22+
};
23+
})();
24+
exports.__esModule = true;
25+
function classExtender(superClass, _instanceModifier) {
26+
return (function (_super) {
27+
__extends(decoratorFunc, _super);
28+
function decoratorFunc() {
29+
var args = [];
30+
for (var _i = 0; _i < arguments.length; _i++) {
31+
args[_i] = arguments[_i];
32+
}
33+
var _this = _super.apply(this, args) || this;
34+
_instanceModifier(_this, args);
35+
return _this;
36+
}
37+
return decoratorFunc;
38+
}(superClass));
39+
}
40+
exports.classExtender = classExtender;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function classExtender<TFunction>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
2+
return class decoratorFunc extends superClass {
3+
constructor(...args: any[]) {
4+
super(...args);
5+
_instanceModifier(this, args);
6+
}
7+
};
8+
}

0 commit comments

Comments
 (0)