我在打字稿如下定義打字稿類型定義如何創建同名的方法
**sample.d.ts**
declare namespace Sample {
class Widget {
name(): string; // gets the name
name(name: string): void; // sets the name
}
}
當我創建以下派生類型,我沒有得到任何警告:
export class BarWidget extends Sample.Widget {
public name(): string {
return '';
}
}
然而,當我創建以下類型,我收到Typescript錯誤:
export class FooWidget extends Sample.Widget {
public name(name: string): void {
}
}
錯誤消息是:
Class 'FooWidget' incorrectly extends base class 'Widget'. Types of property 'name' are incompatable. Type '(name: string) => void' is not assignable to type '{(): string; (name: string): void;}'
更新:
這個問題涉及用於庫如kendo.ui支持:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/kendo-ui/kendo-ui.d.ts#L3373
在該代碼中的文件,將DropDownList的value屬性需要設置並使用以下語法得到:
let foo = dd.value();
dd.value('new value');
我的最終解決方案爲改變kendo.ui.d.ts的定義:
declare namespace Sample {
class Widget {
name(name?: string):string
}
}
在環境名稱空間之外工作。 使用修改後的示例會給出錯誤消息:'一個訪問者不能在環境中聲明' –
再次感謝您的幫助。這可行,但它在技術上不符合要求,因爲它將名稱的基本定義從方法更改爲屬性。 我已經在Typescript回購上添加了一個問題:https://github.com/Microsoft/TypeScript/issues/9907 –
請參閱我的最終評論以獲取其他上下文:https://github.com/Microsoft/TypeScript/issues/9907 –