Add related span for mixin constructor error (#28319)

* Add related span for mixin constructor error

* Remove unneeded casts

* Nicer style
This commit is contained in:
Wesley Wigham 2018-11-02 17:29:05 -07:00 committed by GitHub
parent 92a48d8880
commit eba83f4ea7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 150 additions and 2 deletions

View file

@ -5674,7 +5674,18 @@ namespace ts {
return type.resolvedBaseConstructorType = errorType;
}
if (!(baseConstructorType.flags & TypeFlags.Any) && baseConstructorType !== nullWideningType && !isConstructorType(baseConstructorType)) {
error(baseTypeNode.expression, Diagnostics.Type_0_is_not_a_constructor_function_type, typeToString(baseConstructorType));
const err = error(baseTypeNode.expression, Diagnostics.Type_0_is_not_a_constructor_function_type, typeToString(baseConstructorType));
if (baseConstructorType.flags & TypeFlags.TypeParameter) {
const constraint = getConstraintFromTypeParameter(baseConstructorType);
let ctorReturn: Type = unknownType;
if (constraint) {
const ctorSig = getSignaturesOfType(constraint, SignatureKind.Construct);
if (ctorSig[0]) {
ctorReturn = getReturnTypeOfSignature(ctorSig[0]);
}
}
addRelatedInfo(err, createDiagnosticForNode(baseConstructorType.symbol.declarations[0], Diagnostics.Did_you_mean_for_0_to_be_constrained_to_type_new_args_Colon_any_1, symbolToString(baseConstructorType.symbol), typeToString(ctorReturn)));
}
return type.resolvedBaseConstructorType = errorType;
}
type.resolvedBaseConstructorType = baseConstructorType;

View file

@ -2493,6 +2493,10 @@
"category": "Error",
"code": 2734
},
"Did you mean for '{0}' to be constrained to type 'new (...args: any[]) => {1}'?": {
"category": "Error",
"code": 2735
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",

View file

@ -1,13 +1,36 @@
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(12,5): error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'.
tests/cases/compiler/baseConstraintOfDecorator.ts(12,40): error TS2507: Type 'TFunction' is not a constructor function type.
==== tests/cases/compiler/baseConstraintOfDecorator.ts (2 errors) ====
==== tests/cases/compiler/baseConstraintOfDecorator.ts (4 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.
!!! related TS2735 tests/cases/compiler/baseConstraintOfDecorator.ts:1:31: Did you mean for 'TFunction' to be constrained to type 'new (...args: any[]) => unknown'?
constructor(...args: any[]) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
super(...args);
~~~~~~~~~~~~~~~~~~~~~~~~~~~
_instanceModifier(this, args);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
~~~~~~~~~
};
~~~~~~
!!! error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'.
}
class MyClass { private x; }
export function classExtender2<TFunction extends new (...args: string[]) => MyClass>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
return class decoratorFunc extends superClass {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~
!!! error TS2507: Type 'TFunction' is not a constructor function type.
!!! related TS2735 tests/cases/compiler/baseConstraintOfDecorator.ts:11:32: Did you mean for 'TFunction' to be constrained to type 'new (...args: any[]) => MyClass'?
constructor(...args: any[]) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
super(...args);

View file

@ -7,6 +7,16 @@ export function classExtender<TFunction>(superClass: TFunction, _instanceModifie
}
};
}
class MyClass { private x; }
export function classExtender2<TFunction extends new (...args: string[]) => MyClass>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
return class decoratorFunc extends superClass {
constructor(...args: any[]) {
super(...args);
_instanceModifier(this, args);
}
};
}
//// [baseConstraintOfDecorator.js]
@ -41,3 +51,24 @@ function classExtender(superClass, _instanceModifier) {
}(superClass));
}
exports.classExtender = classExtender;
var MyClass = /** @class */ (function () {
function MyClass() {
}
return MyClass;
}());
function classExtender2(superClass, _instanceModifier) {
return /** @class */ (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.classExtender2 = classExtender2;

View file

@ -27,3 +27,37 @@ export function classExtender<TFunction>(superClass: TFunction, _instanceModifie
};
}
class MyClass { private x; }
>MyClass : Symbol(MyClass, Decl(baseConstraintOfDecorator.ts, 7, 1))
>x : Symbol(MyClass.x, Decl(baseConstraintOfDecorator.ts, 9, 15))
export function classExtender2<TFunction extends new (...args: string[]) => MyClass>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
>classExtender2 : Symbol(classExtender2, Decl(baseConstraintOfDecorator.ts, 9, 28))
>TFunction : Symbol(TFunction, Decl(baseConstraintOfDecorator.ts, 10, 31))
>args : Symbol(args, Decl(baseConstraintOfDecorator.ts, 10, 54))
>MyClass : Symbol(MyClass, Decl(baseConstraintOfDecorator.ts, 7, 1))
>superClass : Symbol(superClass, Decl(baseConstraintOfDecorator.ts, 10, 85))
>TFunction : Symbol(TFunction, Decl(baseConstraintOfDecorator.ts, 10, 31))
>_instanceModifier : Symbol(_instanceModifier, Decl(baseConstraintOfDecorator.ts, 10, 107))
>instance : Symbol(instance, Decl(baseConstraintOfDecorator.ts, 10, 128))
>args : Symbol(args, Decl(baseConstraintOfDecorator.ts, 10, 142))
>TFunction : Symbol(TFunction, Decl(baseConstraintOfDecorator.ts, 10, 31))
return class decoratorFunc extends superClass {
>decoratorFunc : Symbol(decoratorFunc, Decl(baseConstraintOfDecorator.ts, 11, 10))
>superClass : Symbol(superClass, Decl(baseConstraintOfDecorator.ts, 10, 85))
constructor(...args: any[]) {
>args : Symbol(args, Decl(baseConstraintOfDecorator.ts, 12, 20))
super(...args);
>args : Symbol(args, Decl(baseConstraintOfDecorator.ts, 12, 20))
_instanceModifier(this, args);
>_instanceModifier : Symbol(_instanceModifier, Decl(baseConstraintOfDecorator.ts, 10, 107))
>this : Symbol(decoratorFunc, Decl(baseConstraintOfDecorator.ts, 11, 10))
>args : Symbol(args, Decl(baseConstraintOfDecorator.ts, 12, 20))
}
};
}

View file

@ -29,3 +29,38 @@ export function classExtender<TFunction>(superClass: TFunction, _instanceModifie
};
}
class MyClass { private x; }
>MyClass : MyClass
>x : any
export function classExtender2<TFunction extends new (...args: string[]) => MyClass>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
>classExtender2 : <TFunction extends new (...args: string[]) => MyClass>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void) => TFunction
>args : string[]
>superClass : TFunction
>_instanceModifier : (instance: any, args: any[]) => void
>instance : any
>args : any[]
return class decoratorFunc extends superClass {
>class decoratorFunc extends superClass { constructor(...args: any[]) { super(...args); _instanceModifier(this, args); } } : typeof decoratorFunc
>decoratorFunc : typeof decoratorFunc
>superClass : TFunction
constructor(...args: any[]) {
>args : any[]
super(...args);
>super(...args) : void
>super : any
>...args : any
>args : any[]
_instanceModifier(this, args);
>_instanceModifier(this, args) : void
>_instanceModifier : (instance: any, args: any[]) => void
>this : this
>args : any[]
}
};
}

View file

@ -6,3 +6,13 @@ export function classExtender<TFunction>(superClass: TFunction, _instanceModifie
}
};
}
class MyClass { private x; }
export function classExtender2<TFunction extends new (...args: string[]) => MyClass>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
return class decoratorFunc extends superClass {
constructor(...args: any[]) {
super(...args);
_instanceModifier(this, args);
}
};
}