typescript - 擴展 charjs 介面以包含缺少的類型
我正在嘗試為 chartjs 設置 scaleDefaults。我正在使用 @types/chart.js v2.7.22 的型別定義。
Chart.scaleService.updateScaleDefaults('logarithmic', {
ticks: {
callback: function (tick: number, index: number, ticks: number[]) {
// CODE
}
}
但是這會產生一個問題:Property 'scaleService' does not exist on type 'typeof Char'。不過,如果我將它轉型為 any,則可以正常運作。
但我不想把這個轉型成 any,所以我想要擴展 chartjs 的介面,結果是:
interface ChartExtended extends Chart { }
(Chart as ChartExtended).scaleService.updateScaleDefaults('logarithmic', {
ticks: {
callback: function (tick: number, index: number, ticks: number[]) {
// CODE
}
}
但是,我再次遇到類型錯誤:
Type 'typeof Chart' cannot be converted to type 'ChartExtended'.
Property 'config' is misisng in type 'typeof Chart'
當我查看 index.d.ts 時,我注意到有一個單獨的類定義和一個同名的命名空間定義:
declare class Chart {
static readonly Chart: typeof Chart;
// Stuff
}
declare namespace Chart {
// Stuff
}
export = Chart;
export as namespace Chart;
我想創建一個介面,正確擴展圖表,以便我可以定義 scaleService。
我會預期將介面定義為如下:
interface ChartExtended extends Chart {
scaleService?: ScaleService
}