javascript - Typescript 中的 d3-tip 函式庫
我對 TypeScript 還是新手,並且嘗試將 d3-tip 庫 (https://github.com/Caged/d3-tip) 加入到我的專案中。通常,我會使用
declare var [name_of_library]: any;
讓編譯器知道命名空間,但 d3-tip 似乎是注入在 d3 命名空間下:
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module with d3 as a dependency.
define(['d3'], factory)
} else {
// Browser global.
root.d3.tip = factory(root.d3)
}
}(this, function (d3) {
...
在 Typescript 項目中使用 d3-tip 的正確方法是什麼?
答案#1
根據 https://github.com/borisyankov/DefinitelyTyped/blob/master/d3/d3.d.ts 你有
declare var d3: D3.Base;
這意味著要新增到 d3,你只需新增到 D3.Base,所以:
declare module D3 {
export interface Base{
tip: any;
}
}
答案#2
根據目前 d3.d.ts 檔案的結構 (https://github.com/borisyankov/DefinitelyTyped/blob/master/d3/d3.d.ts),你現在可以這樣完成它:
declare module d3 {
export var tip: any;
}
然而,「正確」的解決方案是編寫完整的型別定義檔。上述只是快速讓編譯器不發出錯誤的方法。
