d3Service / d3-ng2-service TypeScript TS2346提供的参数与签名

时间:2017-07-01 04:45:57

标签: javascript typescript d3.js svg

我有一个包含rect元素和文本元素的SVG。

的index.html

<svg id="timeline" width="300" height="100">
    <g transform="translate(10,10)" class="container" width="280" height="96">
        <rect x="10" y="30" width="48" cy="10" r="30" height="60" class="timelineSeries_0" id="day1"></rect>
        <rect x="58" y="30" width="48" cy="10" r="30" height="60" class="timelineSeries_0" id="day2"></rect>
        <rect x="106" y="30" width="48" cy="10" r="30" height="60" class="timelineSeries_0" id="day3"></rect>
        <rect x="154" y="30" width="48" cy="10" r="30" height="60" class="timelineSeries_0" id="day4"></rect>
        <text class="textnumbers" id="day1" x="22.8" y="50">1</text>
        <text class="textnumbers" id="day2" x="70.8" y="50">1</text>
        <text class="textnumbers" id="day3" x="118.8" y="50">1</text>
        <text class="textnumbers" id="day4" x="116.8" y="50">1</text>
    </g>
</svg>

使用d3-ng2-service中的D3Service,我正在执行以下操作:

let day = 1;
let rect_id = "rect#day" + day;
let ele = D3.select<SVGRectElement>(rect_id).node();
let label_id = rect_id.replace("rect", ".textnumbers");
let numberEle = D3.select<TextElement>(label_id);

我在选择时遇到TS2346错误: error TS2346: Supplied parameters do not match any signature of call target.

D3Service的index.d.ts是:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/d3-selection/index.d.ts#L155

在第162行,它说:

select<DescElement extends BaseType>(selector: string): Selection<DescElement, Datum, PElement, PDatum>;

我假设我从index.d.ts获取了错误的元素类型,我应该使用SVGRectElementTextElement之外的其他内容,但我无法弄清楚是什么。 Angular2组件在实践中运行良好。如何阅读index.d.ts文件以查找正确的元素类型?谢谢!

1 个答案:

答案 0 :(得分:1)

正如您在select()方法签名中所看到的那样:

select<DescElement extends BaseType>(selector: string): Selection<DescElement, Datum, PElement, PDatum>;

select方法返回Selection元素,因此转换为SVRectElement不可能。此外,强制转换(如果需要)应该放在这样的方法前面:let ele = <SVGRectElement> D3.select(rect_id).node();

在这种情况下,虽然没有必要进行强制转换,但代码应为:

let rectEle = self.d3.select(rectId);