admin管理员组

文章数量:1323715

I can't get my head around the following TypeScript error message in the Webstorm IDE (pretty new to TypeScript so sorry ...)

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:

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

I can't get my head around the following TypeScript error message in the Webstorm IDE (pretty new to TypeScript so sorry ...)

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:

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

Share Improve this question asked Oct 24, 2013 at 14:21 dorjeduckdorjeduck 7,80411 gold badges53 silver badges68 bronze badges 2
  • That example (stripped down) works for me on typescriptlang/Playground – SLaks Commented Oct 24, 2013 at 14:27
  • The Playground and WebStorm aren't necessarily (probably aren't) using the same releases of TypeScript. Try explicitly typing the function passed into outerRadius and see what happens. outerRadius(function (d: any): number { return d - 9; }); – Jeffery Grajkowski Commented Oct 24, 2013 at 15:12
Add a ment  | 

1 Answer 1

Reset to default 4

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());.

Just note the <string> before the parameter.

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 Function and function.

本文标签: javascriptTypeScriptArgument Type Function is not assignable to parameter type functionStack Overflow