【问题标题】:Property 'tip' does not exist on type 'typeof @types/d3/index"'类型“typeof @types/d3/index”上不存在属性“tip”
【发布时间】:2017-11-03 11:31:09
【问题描述】:

我无法使用 d3 提示。

import * as d3 from "d3";
import  "d3-tip";

var tool_tip = d3.tip()

我收到这样的错误。

Property 'tip' does not exist on type 'typeof "/home/viktor/projects/internet_trading_platform/client/node_modules/@types/d3/index"'.

我已导入 d3 提示模块,必须扩充 d3 模块。但事实并非如此。根据https://www.typescriptlang.org/docs/handbook/declaration-merging.html

问候。

d3-tip 声明位于此处https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/d3-tip/index.d.ts

【问题讨论】:

    标签: typescript d3.js


    【解决方案1】:

    试试这个。

    import d3Tip from "d3-tip"
    
    const tip = d3Tip();
    
    tip.attr("class", "d3-tip")
       .html(d => { return "html"})
    

    【讨论】:

      【解决方案2】:

      不是最好的答案,但对于遇到此问题的任何人来说,对我有用的解决方案是

      正常导入 d3Tip

      import * as d3Tip from 'd3-tip';
      

      然后在初始化提示时

      var tipObject = (<any>d3Tip)()
            .attr('class', 'd3-tip')
            .html('Loading...');
      

      【讨论】:

        【解决方案3】:

        已经得到这个很长一段时间了,只是想了一个解决方法:

        import * as d3 from 'd3';
        
        Object.defineProperty(d3, 'tip', {
          value: require('d3-tip')
        });
        

        或者,如果您根本不喜欢 require() 语句:

        import * as d3 from 'd3';
        import * as d3Tip from 'd3-tip';
        
        Object.defineProperty(d3, 'tip', {
          value: d3Tip
        });
        

        这样 TypeScript 就不会再抱怨了。 它也适用于 @types/d3@3@types/d3-tip@3

        【讨论】:

          猜你喜欢
          • 2019-02-02
          • 2017-06-28
          • 2021-07-05
          • 2021-01-09
          • 1970-01-01
          • 2020-10-04
          • 2020-08-19
          • 2017-11-06
          • 2018-11-09
          相关资源
          最近更新 更多