typescript TS - 只能使用“new”关键字调用 void 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43484360/
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
TS - Only a void function can be called with the 'new' keyword
提问by Alexander Mills
I am getting this weird error from TypeScript:
我从 TypeScript 收到这个奇怪的错误:
"Only a void function can be called with the 'new' keyword."
“只能使用 'new' 关键字调用 void 函数。”
What?
什么?
The constructor function, just looks like:
构造函数,看起来像:
function Suman(obj: ISumanInputs): void {
const projectRoot = _suman.projectRoot;
// via options
this.fileName = obj.fileName;
this.slicedFileName = obj.fileName.slice(projectRoot.length);
this.networkLog = obj.networkLog;
this.outputPath = obj.outputPath;
this.timestamp = obj.timestamp;
this.sumanId = ++sumanId;
// initialize
this.allDescribeBlocks = [];
this.describeOnlyIsTriggered = false;
this.deps = null;
this.numHooksSkipped = 0;
this.numHooksStubbed = 0;
this.numBlocksSkipped = 0;
}
I have no idea what the problem is. I tried adding and removing the return type (void) but that did nothing.
我不知道问题是什么。我尝试添加和删除返回类型(void),但没有任何作用。
采纳答案by Sean Vieira
The issue is that ISumanInputs
does not include one or more of the properties that you're including in your call orthat you haven't properly fulfilled the IsumanInputs
interface.
问题是ISumanInputs
不包括您在调用中包含的一个或多个属性,或者您没有正确实现IsumanInputs
接口。
In the extra property case you should get one "extra" error:
在额外的财产情况下,你应该得到一个“额外”的错误:
Object literal may only specify known properties, and 'anExtraProp' does not exist in type 'ISumanInputs'
对象字面量只能指定已知属性,并且类型“ISumanInputs”中不存在“anExtraProp”
In the missing property case you will get a different "extra" error:
在缺少属性的情况下,您将收到不同的“额外”错误:
Property 'timestamp' is missing in type '{ fileName: string; networkLog: string; outputPath: string; }'.
类型“{ fileName: string;”中缺少属性“timestamp” 网络日志:字符串;输出路径:字符串;}'。
Interestingly enough, if you move the definition of the argument out of line the extra propertycase no longer fails:
有趣的是,如果您将参数的定义移出线外,额外的属性案例不再失败:
const data = {
fileName: "abc",
networkLog: "",
outputPath: "",
timestamp: "",
anExtraProperty: true
};
new Suman(data);
回答by Jokester
As Sean pointed out, this is a less obvious consequence of type mismatch in arguments.
正如肖恩所指出的,这是参数类型不匹配的一个不太明显的结果。
In case a deeper reason interests you: when arguments of a function don't typecheck, tsc
infers return type to be a special type never
(overriding void
you specified). And new
with such a function will cause TS2350 Only a void function can...
.
如果您对更深层次的原因感兴趣:当函数的参数不进行tsc
类型检查时,将返回类型推断为特殊类型never
(覆盖void
您指定的类型)。而new
有了这样的功能就会造成TS2350 Only a void function can...
.
This snippet can trigger TS2350 without incorrect arguments.
此代码段可以在没有错误参数的情况下触发 TS2350。
function Ctor(): never {
throw "never return";
}
const v = new Ctor();