TypeScript - 建立 .d.ts 定義檔 - 嵌套函式
我正在為 RiotJS 創建 TypeScript 定義。這個庫使用像這樣的巢狀函數:
riot.route( args ); // function
riot.route.parser( args ); // function in function
這是我目前為止的摘錄:
declare module riot
{
export function route( callback : Function ) : void
export function route( to : string ) : void
}
我不確定如何為「riot.route.parser()」巢狀函數構建定義,也想知道是否有人有任何見解可以分享?
謝謝
答案#1
這是一個使用內聯打字的實現:
declare var riot: {
route: {
(callback: Function): void
parser: {
(args): any;
}
};
}
riot.route(() => null); // function
riot.route.parser(1); // function in function
我建議你將它分成介面,並根據 a.) 目的 或 b.) 實際文件如何稱呼它們 來命名它們。
一個讓你開始的範例:
interface Riot {
route: {
(callback: Function): void
parser: {
(args): any;
}
};
}
declare var riot:Riot;
riot.route(() => null); // function
riot.route.parser(1); // function in function
更新
對於返回類型/參數數量不變的函數重載,我建議使用聯合類型:
interface Riot {
route: {
(toOrCallback: string|Function): void
(callback: Function): void
parser: {
(args): any;
}
};
}
