全局示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| function globalLib(option) { console.log(option); } globalLib.version = '1.0.0'; globalLib.doSomething = function () { console.log('global do something'); };
declare function globalLib(option: globalLib.Option): void;
declare namespace globalLib { const version: string; function doSomething(): void; interface Option { [key: string]: any; } }
declare function globalLib(option: globalLib.Option): void;
declare namespace globalLib { const version: string; function doSomething(): void; interface Option { [key: string]: any; } }
|
模块示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| const version = '1.0.0' const doSomething = function() { console.log('module do something') } function moduleLib(option) { console.log(option) }
moduleLib.version = version moduleLib.doSomething = doSomething
module.exports = moduleLib
declare function moduleLib(option: Option): void
interface Option { [key: string]: any }
declare namespace moduleLib { const version: string function doSomething(): void }
export = moduleLib
umd 模块示例
(function (root, factory) { if (typeof define === 'function' && define.amd) { define(factory) } else if (typeof module === 'object' && module.exports) { module.exports = factory() } else { root.umdLib = factory() } }(this, function() { return { version: '1.0.0', doSomething() { console.log('umd do something') } } }))
declare namespace umdLib { const version: string function doSomething(): void }
export as namespace umdLib
export = umdLib
|
前置知识:
declare var 声明全局变量
declare function 声明全局方法
declare class 声明全局类
declare enum 声明全局枚举类型
declare namespace 声明(含有子属性的)全局对象
interface 和 type 声明全局类型
export 导出变量
export namespace 导出(含有子属性的)对象
export default ES6 默认导出
export = commonjs 导出模块
export as namespace UMD 库声明全局变量
declare global 扩展全局变量
declare module 扩展模块
/// 三斜线指令