TypeScript 模块
本节将介绍 TypeScript 的模块化方案,学习模块的导入导出机制,要注意 TypeScript 是怎么样兼容 Commonjs 和 AMD 规范的。
1. 解释
模块在其自身的作用域里执行,而不是在全局作用域里。
export
: 导出模块中的变量、函数、类、接口等;
import
: 导入其他模块导出的变量、函数、类、接口等。
TypeScript 与 ECMAScript 2015 一样,任何包含顶级 import
或者 export
的文件都被当成一个模块。相反的,如果一个文件不带有顶级的 import
或者 export
声明,那么它的内容被视为全局可见的。
2. 全局模块
const a =
const a =
如果加上 export
导出语句:
export const a =
这样,两个 a
因处于不同的命名空间,就不会报错。
3. 导出语法
3.1 使用 export 导出声明
export.ts
:
export const a: number =
export const add = (x: number, y:number) => x + y
export interface User {
nickname: string,
department: string
}
export class Employee implements User {
public nickname!: string
public department!: string
}
export type used = true | false
解释: 每个声明都通过 export
关键字导出。
3.2 先声明,后导出
const a: number =
const add = (x: number, y:number) => x + y
interface User {
nickname: string,
department: string
}
class Employee implements User {
public nickname!: string
public department!: string
}
type used = true | false
export { a, add, Employee }
解释: 先进行声明操作,最终统一使用 export
关键字导出。
3.3 导出时重命名
const a: number =
const add = (x: number, y:number) => x + y
interface User {
nickname: string,
department: string
}
class Employee implements User {
public nickname!: string
public department!: string
}
type used = true | false
export { add }
export { a as level, used as status, Employee }
解释: 在导出时,可以用 as
关键字将声明重命名。
3.4 重新导出
ZipCodeValidator.ts
:
export interface StringValidator {
isAcceptable(s: string): boolean
}
export const numberRegexp = /^[0-9]+$/
class ZipCodeValidator implements StringValidator {
isAcceptable(s: string) {
return s.length === && numberRegexp.test(s)
}
}
export { ZipCodeValidator }
export { ZipCodeValidator as mainValidator }
ParseIntBasedZipCodeValidator.ts
:
export class ParseIntBasedZipCodeValidator {
isAcceptable(s: string) {
return s.length === && parseInt(s).toString() === s
}
}
// 导出原先的验证器但做了重命名
export {ZipCodeValidator as RegExpBasedZipCodeValidator} from './ZipCodeValidator'
比如在 validator.ts
文件中,统一导出这两个模块。
// validator.ts
export * from './ZipCodeValidator'
export * from './ParseIntBasedZipCodeValidator'
3.5 默认导出
export default class ZipCodeValidator {
static numberRegexp = /^[0-9]+$/
isAcceptable(s: string) {
return s.length === && ZipCodeValidator.numberRegexp.test(s)
}
}
4. 导入语法
4.1 使用 import 导入
使用 import
形式来导入其它模块中的导出内容。
import { a, add, Employee } from './export'
4.2 导入时重命名
import { a as level, used as status } from './export'
4.3 将整个模块导入到一个变量
将整个模块导入到一个变量,并通过它来访问模块的导出部分
import * as TYPES from './export'
4.4 直接导入
import './export'
5. export =
和 import = require()
Commonjs 和 AMD 的 exports
都可以被赋值为一个 对象
, 这种情况下其作用就类似于 EcmaScript 2015 语法里的默认导出,即 export default
语法了。虽然作用相似,但是 export default
语法并不能兼容 Commonjs 和 AMD 的 exports
。
若使用 export =
导出一个模块,则必须使用 TypeScript 的特定语法 import module = require('module')
来导入此模块。
-
export =
只能导出对象
-
export =
导出的模块只能用import = require()
形式导入
文件 ZipCodeValidator.ts
:
let numberRegexp = /^[0-9]+$/
class ZipCodeValidator {
isAcceptable(s: string) {
return s.length === && numberRegexp.test(s)
}
}
export = ZipCodeValidator
文件 Test.ts
:
import Zip = require('./ZipCodeValidator')
// Some samples to try
let strings = ['Hello', '98052', '101']
// Validators to use
let validator = new Zip()
// Show whether each string passed each validator
strings.forEach(s => {
console.log(`'${ s }' - ${ validator.isAcceptable(s) ? 'matches' : 'does not match' }`)
});
代码解释: 通过 import = require()
形式导入。