Merge pull request #14250 from Microsoft/fix-crash-in-isConstructorType

isConstructorType checks base constraint for undefined
This commit is contained in:
Nathan Shively-Sanders 2017-02-22 14:31:33 -08:00 committed by GitHub
commit 746c45ccd9
4 changed files with 72 additions and 1 deletions

View file

@ -3952,7 +3952,7 @@ namespace ts {
}
if (type.flags & TypeFlags.TypeVariable) {
const constraint = getBaseConstraintOfType(<TypeVariable>type);
return isValidBaseType(constraint) && isMixinConstructorType(constraint);
return constraint && isValidBaseType(constraint) && isMixinConstructorType(constraint);
}
return false;
}

View file

@ -0,0 +1,23 @@
tests/cases/compiler/baseConstraintOfDecorator.ts(2,5): error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'.
tests/cases/compiler/baseConstraintOfDecorator.ts(2,40): error TS2507: Type 'TFunction' is not a constructor function type.
==== tests/cases/compiler/baseConstraintOfDecorator.ts (2 errors) ====
export function classExtender<TFunction>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
return class decoratorFunc extends superClass {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~
!!! error TS2507: Type 'TFunction' is not a constructor function type.
constructor(...args: any[]) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
super(...args);
~~~~~~~~~~~~~~~~~~~~~~~~~~~
_instanceModifier(this, args);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
~~~~~~~~~
};
~~~~~~
!!! error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'.
}

View file

@ -0,0 +1,40 @@
//// [baseConstraintOfDecorator.ts]
export function classExtender<TFunction>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
return class decoratorFunc extends superClass {
constructor(...args: any[]) {
super(...args);
_instanceModifier(this, args);
}
};
}
//// [baseConstraintOfDecorator.js]
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
exports.__esModule = true;
function classExtender(superClass, _instanceModifier) {
return (function (_super) {
__extends(decoratorFunc, _super);
function decoratorFunc() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var _this = _super.apply(this, args) || this;
_instanceModifier(_this, args);
return _this;
}
return decoratorFunc;
}(superClass));
}
exports.classExtender = classExtender;

View file

@ -0,0 +1,8 @@
export function classExtender<TFunction>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
return class decoratorFunc extends superClass {
constructor(...args: any[]) {
super(...args);
_instanceModifier(this, args);
}
};
}