'new' 表达式,其目标在 TypeScript 中缺少构造签名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43623461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'new' expression, whose target lacks a construct signature in TypeScript
提问by fulvio
We have the following TestComponent.ts
TypeScript class:
我们有以下TestComponent.ts
TypeScript 类:
01: import TestVectorLayer from './TestVectorLayer'
02:
03: export class TestComponent implements OnInit {
04: private foo: any;
05:
06: constructor() { }
07:
08: const layer = new TestVectorLayer("foo");
09: }
And the following TestVectorLayer.ts
function:
以及以下TestVectorLayer.ts
功能:
Keep in mind that OpenLayer's 3 is using the Google Closure Library, that's why TestVectorLayer
is not a TypeScript class.
请记住,OpenLayer 的 3 使用的是 Google Closure Library,这就是为什么TestVectorLayer
它不是 TypeScript 类的原因。
01: declare let ol: any;
02:
03: const TestVectorLayer = function (layerName: string) {
04: ...
05: console.log(layerName);
06:
07: ol.layer.Image.call(this, opts);
08: }
09:
10: ol.inherits(TestVectorLayer as any, ol.layer.Image as any);
11:
12: export default TestVectorLayer;
We're getting the following error:
我们收到以下错误:
Error on Line 08 in TestComponent.ts class:
[ts] 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type. import TestVectorLayer
[ts] 'new' 表达式,其目标缺少构造签名,隐式具有 'any' 类型。导入测试向量层
The package.json
versions of TypeScript:
package.json
TypeScript的版本:
devDependencies:
开发依赖:
"typescript": "~2.2.1"
回答by David Sherret
Here's a simplification of the question:
这是问题的简化:
const TestVectorLayer = function(layerName: string) {
};
const layer = new TestVectorLayer("");
The error is happening because TestVectorLayer
doesn't have a new signature, so layer
is implicitly typed as any
. That errors with --noImplicitAny
.
发生错误是因为TestVectorLayer
没有新签名,因此layer
隐式键入为any
. 错误与--noImplicitAny
.
You can fix this by switching to a class, but in your case this seems a bit more complicated because the inheritance is done by the underlying framework. Because of that, you will have to do something a bit more complicated and it's not ideal:
您可以通过切换到类来解决此问题,但在您的情况下,这似乎有点复杂,因为继承是由底层框架完成的。因此,您将不得不做一些更复杂的事情,这并不理想:
interface TestVectorLayer {
// members of your "class" go here
}
const TestVectorLayer = function (this: TestVectorLayer, layerName: string) {
// ...
console.log(layerName);
ol.layer.Image.call(this, opts);
} as any as { new (layerName: string): TestVectorLayer; };
ol.inherits(TestVectorLayer, ol.layer.Image);
export default TestVectorLayer;
Then in the file with TestComponent
:
然后在文件中TestComponent
:
const layer = new TestVectorLayer(layerName); // no more compile error
回答by Mariusz Pawelski
David answer is great, but if you care just about quickly making it compile (for example because you are migrating from JS to TS) then you can simply put any
there to shut up complaining compiler.
David 的回答很棒,但是如果您只关心快速编译它(例如,因为您正在从 JS 迁移到 TS),那么您可以简单地把any
它放在那里,让抱怨的编译器闭嘴。
TS file:
TS文件:
const TestConstructorFunction = function (this: any, a: any, b: any) {
this.a = a;
this.b = b;
};
let test1 = new (TestConstructorFunction as any)(1, 2);
compiles to this JS file:
编译成这个JS文件:
var TestConstructor = function (a, b) {
this.a = a;
this.b = b;
};
var test1 = new TestConstructor(1, 2);
Just pay attention to not make this mistake:
只要注意不要犯这个错误:
TS file:
TS文件:
// wrong!
let test2 = new (TestConstructorFunction(1, 2) as any);
JS result:
JS结果:
// wrong!
var test2 = new (TestConstructor(1, 2));
and this is wrong. You'll get TypeError: TestConstructor(...) is not a constructor
error at runtime.
这是错误的。你会TypeError: TestConstructor(...) is not a constructor
在运行时得到错误。