typescript ES6:模块

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript ES6:模块相关的知识,希望对你有一定的参考价值。

/// <reference path="Validation.ts" />
module Validation {
  var numberRegexp = /^[0-9]+$/;
  export class ZipCodeValidator implements StringValidator {
    isAcceptable(s:string) {
      return s.length === 5 && numberRegexp.test(s);
    }
  }
}
module Validation {
  export interface StringValidator {
    isAcceptable(s: string): boolean;
  }
}
/// <reference path="Validation.ts" />
/// <reference path="LettersOnlyValidator.ts" />
/// <reference path="ZipCodeValidator.ts" />

// samples to try
var strings = ['Hello', '90210', '101'];
// validators to use
var validators: { [s: string]: Validation.StringValidator } = {};
validators['ZIP code'] = new Validation.ZipCodeValidator();
validators['Letters only'] = new Validation.LettersOnlyValidator();
// show whether each string passed each validator
strings.forEach(s => {
  for (var name in validators) {
    console.log('"' + s + '" ' + (validators[name].isAcceptable(s) ? ' matches ' : ' does not match ') + name);
  }
});

/// <reference path="Validation.ts" />
module Validation {
  var lettersRegexp = /^[A-Za-z]+$/;
  export class LettersOnlyValidator implements StringValidator {
    isAcceptable(s:string) {
      return lettersRegexp.test(s);
    }
  }
}

以上是关于typescript ES6:模块的主要内容,如果未能解决你的问题,请参考以下文章

jest + typescript + es6 模块(又一次,2019 年)- SyntaxError: Unexpected token export

typescript ES6:外部模块

typescript ES6:模块

ES6 模块、VueJS 和 TypeScript

使用 TypeScript ES6 模块和 RequireJS 注册 Angular 元素

Typescript 实战 --- ES6与CommonJS的模块系统