相关文章快速入口:鸿蒙ArkTS语言快速入门-TS(四)
TS入门学习第五篇
- TS入门学习第五篇
- 模块
- 导出模块 exports
- 导入模块 imports
- 外部模块
- 命名空间
TS入门学习第五篇
模块
模块在其自身的作用域里执行,而不是在全局作用域里;这意味着定义在一个模块里的变量,函数,类等等在模块外部是不可见的,除非你明确地使用export形式之一导出它们。 相反,如果想使用其它模块导出的变量,函数,类,接口等的时候,你必须要导入它们,可以使用import形式之一。
模块是自声明的;两个模块之间的关系是通过在文件级别上使用imports和exports建立的。
导出模块 exports
任何声明(比如变量,函数,类,类型别名或接口)都能够通过添加export关键字来导出。
export interface StringValidator {isAcceptable(s: string): boolean;
}
export class ZipCodeValidator implements StringValidator {isAcceptable(s: string) {return s.length === 5 && numberRegexp.test(s);}
}export class ParseIntBasedZipCodeValidator {isAcceptable(s: string) {return s.length === 5 && parseInt(s).toString() === s;}
}// 导出原先的验证器但做了重命名
export {ZipCodeValidator as RegExpBasedZipCodeValidator} from "./ZipCodeValidator";
默认导出
每个模块都可以有一个default导出。 默认导出使用default关键字标记;并且一个模块只能够有一个default导出。 需要使用一种特殊的导入形式来导入default导出。
export default class ZipCodeValidator {static numberRegexp = /^[0-9]+$/;isAcceptable(s: string) {return s.length === 5 && ZipCodeValidator.numberRegexp.test(s);}
}
//Test.ts 类和函数声明可以直接被标记为默认导出。 标记为默认导出的类和函数的名字是可以省略的。
import validator from "./ZipCodeValidator";let myValidator = new validator();
导入模块 imports
模块的导入操作与导出一样简单。 可以使用以下import形式之一来导入其它模块中的导出内容。
import { ZipCodeValidator } from "./ZipCodeValidator";let myValidator = new ZipCodeValidator();//将整个模块导入到一个变量,并通过它来访问模块的导出部分
import * as validator from "./ZipCodeValidator";
let myValidator = new validator.ZipCodeValidator();
外部模块
在Node.js里大部分工作是通过加载一个或多个模块实现的。 我们可以使用顶级的export声明来为每个模块都定义一个.d.ts文件,但最好还是写在一个大的.d.ts文件里。 我们使用与构造一个外部命名空间相似的方法,但是这里使用module关键字并且把名字用引号括起来,方便之后import。
node.d.ts (simplified excerpt)
declare module "url" {export interface Url {protocol?: string;hostname?: string;pathname?: string;}export function parse(urlStr: string, parseQueryString?, slashesDenoteHost?): Url;
}declare module "path" {export function normalize(p: string): string;export function join(...paths: any[]): string;export let sep: string;
}
在我们可以/// node.d.ts并且使用import url = require(“url”);或import * as URL from "url"加载模块。
/// <reference path="node.d.ts"/>
import * as URL from "url";
let myUrl = URL.parse("http://www.typescriptlang.org");
命名空间
任何使用module关键字来声明一个内部模块的地方都应该使用namespace关键字来替换。 这就避免了让新的使用者被相似的名称所迷惑。
我们想让这些接口和类在命名空间之外也是可访问的,所以需要使用export。 相反的,变量lettersRegexp和numberRegexp是实现的细节,不需要导出,因此它们在命名空间外是不能访问的。 在文件末尾的测试代码里,由于是在命名空间之外访问,因此需要限定类型的名称。
namespace Validation {export interface StringValidator {isAcceptable(s: string): boolean;}
}/// <reference path="Validation.ts" />
namespace Validation {const lettersRegexp = /^[A-Za-z]+$/;export class LettersOnlyValidator implements StringValidator {isAcceptable(s: string) {return lettersRegexp.test(s);}}
}/// <reference path="Validation.ts" />
namespace Validation {const numberRegexp = /^[0-9]+$/;export class ZipCodeValidator implements StringValidator {isAcceptable(s: string) {return s.length === 5 && numberRegexp.test(s);}}
}
使用说明
// Some samples to try
let strings = ["Hello", "98052", "101"];// Validators to use
let validators: { [s: string]: Validation.StringValidator; } = {};
validators["ZIP code"] = new Validation.ZipCodeValidator();
validators["Letters only"] = new Validation.LettersOnlyValidator();// Show whether each string passed each validator
for (let s of strings) {for (let name in validators) {console.log(""" + s + "" " + (validators[name].isAcceptable(s) ? " matches " : " does not match ") + name);}
}