网站搜索

javascript - d3.js Typescript 依赖编译错误


我正在使用 Node.js、Angular、TypeScript 和 d3.js(以及其他一些技术)设计一个 Web 应用程序。该应用程序已经启动并运行,库功能也按预期工作。然而,我无法正确编译我的 TypeScript 文件,因为在 d3 的类型定义上出现了错误(见下文)。

Error:(182, 27) TS2339:Property 'time' does not exist on type 'typeof 
  "C:/Users/my.user/project/workspace/node_modules/@types/d3/index"'.
Error:(187, 27) TS2339:Property 'scale' does not exist on type 'typeof 
  "C:/Users/my.user/project/workspace/node_modules/@types/d3/index"'.
Error:(193, 28) TS2339:Property 'svg' does not exist on type 'typeof 
  "C:/Users/my.user/project/workspace/node_modules/@types/d3/index"'.
Error:(195, 47) TS2339:Property 'time' does not exist on type 'typeof 
  "C:/Users/my.user/project/workspace/node_modules/@types/d3/index"'.
Error:(258, 28) TS2339:Property 'svg' does not exist on type 'typeof 
  "C:/Users/my.user/project/workspace/node_modules/@types/d3/index"'.
Error:(915, 23) TS2339:Property 'svg' does not exist on type 'typeof 
  "C:/Users/my.user/project/workspace/node_modules/@types/d3/index"'.
Error:(970, 25) TS2339:Property 'svg' does not exist on type 'typeof 
  "C:/Users/my.user/project/workspace/node_modules/@types/d3/index"'.
Error:(1147, 21) TS2339:Property 'svg' does not exist on type 'typeof 
  "C:/Users/my.user/project/workspace/node_modules/@types/d3/index"'.

这些属性似乎工作正常,但 TypeScript 编译器无法识别它们,这表明我的 @types/d3 依赖有问题。

我使用的 d3.js 版本是 3.4.8,而 @types/d3 的版本是 4.3.0。

我确定这两个版本不匹配,但我仍然需要找到一对匹配的版本号,同时还要提供 Typescript 无法识别的属性。

有人能推荐一个适合这些库的组合,以满足我需要访问的属性吗?我假设我的 d3 类型定义在某些方面有些落后,所以可能使用旧版本会更合适?如果有人有答案,或者有一种系统的方法来找出我需要的版本,那将非常有帮助。

答案#1

我知道这个问题几个月前已经有人提出来过,但它仍然需要被回答。

首先检查 package.json 文件中的 d3 和 @types 版本。 正如你所说,

  • 你正在使用 d3 v3.4.8,所以你应该使用 @types/[email 。
  • 你正在使用 @types/4.3.0,这与 d3 v4.3.0 兼容。

注意:始终服用相同版本的D3和类型。

要搜索类型,请访问微软官方网站。