Typescript 的 d.ts 文件規範

參考:  https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html  

0. 僅在 d.ts 文件中使用的,只需要 declare 聲明一下  

1. 將模塊導出爲全局變量,使用如下方法  

````

/*~ If this module is a UMD module that exposes a global variable 'myLib' when

*~ loaded outside a module loader environment, declare that global here.

*~ Otherwise, delete this declaration. */

export as namespace myLib;

````

 

2. 導出函數

````

/*~ If this module has methods, declare them as functions like so. */

export function myMethod(a: string): string;

export function myOtherMethod(a: number): number;

````

3.導出變量

````

/*~ If the module also has properties, declare them here. For example,

*~ this declaration says that this code is legal:

*~ import f = require('myFuncLibrary');

*~ console.log(f.defaultName); */

export const defaultName: string;

export let defaultLength: number;

````

4.導出類

````

/*~ Write your module's methods and properties in this class */

export class MyClass {

constructor(someParam?: string);

someProperty: string[];

myMethod(opts: MyClass.MyClassMethodOptions): number;

}

````

5.使用 namespace點 訪問的元素需要聲明在 namespace 中

````

/*~ If you want to expose types from your module as well, you can

*~ place them in this block.

*~

*~ Note that if you decide to include this namespace, the module can be

*~ incorrectly imported as a namespace object, unless

*~ --esModuleInterop is turned on:

*~ import * as x from '[~THE MODULE~]'; // WRONG! DO NOT DO THIS! */

declare namespace MyClass {

export interface MyClassMethodOptions { width?: number; height?: number; }

}

````

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章