建立一個匯出預設值的模組
對於更復雜的專案,或者在你打算逐漸鍵入依賴項的情況下,建立模組可能更簡潔。
使用 JQuery(儘管它確實有可用的型別 )作為示例:
// place in jquery.d.ts
declare let $: any;
export default $;
然後在專案的任何檔案中,你可以使用以下命令匯入此定義:
// some other .ts file
import $ from "jquery";
在匯入之後,$
將被輸入為 any
。
如果庫有多個頂級變數,則按名稱匯出和匯入:
// place in jquery.d.ts
declare module "jquery" {
let $: any;
let jQuery: any;
export { $ };
export { jQuery };
}
然後,你可以匯入並使用這兩個名稱:
// some other .ts file
import {$, jQuery} from "jquery";
$.doThing();
jQuery.doOtherThing();