TypeScript:参数类型函数不可分配给参数类型函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19568602/
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
TypeScript : Argument Type Function is not assignable to parameter type function
提问by dorjeduck
I can't get my head around the following TypeScript error message in the Webstorm IDE (pretty new to TypeScript so sorry ...)
我无法理解 Webstorm IDE 中的以下 TypeScript 错误消息(对 TypeScript 来说很新,很抱歉……)
I am using d3.d.ts from the DefinitelyTyped repository to implement graphics with d3js in which the interface for d3.svg.arc() method is defined like:
我正在使用绝对类型存储库中的 d3.d.ts 来实现带有 d3js 的图形,其中 d3.svg.arc() 方法的接口定义如下:
export interface Arc {
...
outerRadius: {
(): (data: any, index?: number) => number;
(radius: number): Arc;
(radius: () => number): Arc;
(radius: (data: any) => number): Arc;
(radius: (data: any, index: number) => number): Arc;
};
...
}
when implementing the arc in the following manner ...
当以下列方式实现弧时......
svg.selectAll(".arc")
.attr("d", function (d) {
return d3.svg.arc().outerRadius(
function (d) {
return d - 9;
})
...
I get the error: "Argument Type Function is not assignable to parameter type function"
我收到错误消息:“参数类型函数不可分配给参数类型函数”
So
所以
function (d) {return d - 9;})
doesn't seem to be valid for that interface even so it seems of the type
似乎对该接口无效,即使它似乎是该类型
(): (data: any, index?: number) => number;
I guess I oversee something obvious yet ... I stuck right now
我想我监督了一些明显的事情......我现在卡住了
Thanks
谢谢
回答by diosney
I had a similar issue with a string to number conversion and fixed it by adding an explicit typecasting like:
我在字符串到数字转换方面遇到了类似的问题,并通过添加显式类型转换来修复它,例如:
$.replace(/text/g, <string>$.now());
.
$.replace(/text/g, <string>$.now());
.
Just note the <string>
before the parameter.
只需注意<string>
前面的参数。
Just try to typecast to and tell how it goes.
只需尝试键入并告诉它是如何进行的。
In your case it will be something like:
在您的情况下,它将类似于:
svg.selectAll(".arc")
.attr("d", function (d) {
return d3.svg.arc().outerRadius(
<Function>function (d) {
return d - 9;
})
...
or maybe:
或许:
svg.selectAll(".arc")
.attr("d", function (d) {
return d3.svg.arc().outerRadius(
<function>function (d) {
return d - 9;
})
...
since the error lies in the difference between Functionand function.
因为错误在于Function和function之间的差异。