我正在将一些JavaScript代码转换为TypeScript。这是一个很酷的JavaScript函数,它使用d3并完美地包装了一个svg文本块。通常我只需要将单词 "function" 更改为 "private",该函数就可以在TypeScript中按原样工作,但是此函数仅对 getComputedTextLength() 函数发出警告。希望有人能解释一下如何使这个函数在TypeScript中正常工作,包括为什么会出现错误。Visual Studio没有提供任何帮助。谢谢。
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
x = text.attr("x"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan")
.attr("x", x).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan")
.attr("x", x).attr("y", y)
.attr("dy", ++lineNumber * lineHeight + dy + "em")
.text(word);
}
}
});
}