简体   繁体   English

TypeScript声明文件

[英]TypeScript declaration file

I'm trying to do a word cloud using typescript, react, d3. 我正在尝试使用打字稿,react,d3做一个词云。

My console complains that d3-cloud from npm needs a declaration file, so I've come up with this: EDIT3: Declaration file now looks like this, the same error message is given: 我的控制台抱怨npm的d3-cloud需要声明文件,因此我想出了这个:EDIT3:声明文件现在看起来像这样,给出了相同的错误消息:

declare module 'd3-cloud' {
  export function cloud (): Function;
}

All I get now is this error: 我现在得到的只是这个错误:

TS2349: Cannot invoke an expression whose type lacks a call signature. TS2349:无法调用类型缺少调用签名的表达式。 Type 'typeof 'd3-cloud'' has no compatible call signatures. 类型'typeof'd3-cloud'没有兼容的呼叫签名。

Thanks for any help! 谢谢你的帮助!

EDIT: I might add that once I reload the page there is a flash of the rendered word cloud, right before the error message takes over. 编辑:我可能会补充说,一旦我重新加载页面,就在错误消息接管之前立即呈现了呈现的文字云。 So it sorta works, but... That AT-loader is really unhappy. 所以它有点用,但是...那个AT-loader真的很不高兴。

EDIT2: I used the npm module like this: EDIT2:我像这样使用npm模块:

import * as d3Cloud from 'd3-cloud'

const layout = d3Cloud()
  .size([width, height])
  .font(font)
  ...

Since the library you use directly exports a function you can type it like this: 由于您使用的库直接导出了一个函数,因此您可以像这样键入它:

declare module 'd3-cloud' {
  let cloud: Function;
  export = cloud;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM