typescript ES6:外部模块

Posted

tags:

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

import validation = require('./Validation');
var numberRegexp = /^[0-9]+$/;
class ZipCodeValidator implements validation.StringValidator {
  isAcceptable(s: string) {
    return s.length === 5 && numberRegexp.test(s);
  }
}

export = ZipCodeValidator;
export interface StringValidator {
  isAcceptable(s: string): boolean;
}
import validation = require('./Validation');
import zipValidator = require('./ZipCodeValidator');
import lettersValidator = require('./LettersOnlyValidator');

// Some samples to try
var strings = ['Hello', '98052', '101'];
// Validators to use
var validators: { [s: string]: validation.StringValidator; } = {};
validators['ZIP code'] = new zipValidator();
validators['Letters only'] = new lettersValidator();
// 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);
    }
});
import validation = require('./Validation');
var lettersRegexp = /^[A-Za-z]+$/;
class LetterOnlyValidator implements validation.StringValidator {
  isAcceptable(s: string) {
    return lettersRegexp.test(s);
  }
}

export = LettersOnlyValidator;

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

TypeScript入门八:TypeScript的命名空间

es6模块 nodejs模块和 typescript模块

Typescript和Node模块解析策略

Typescript es6导入模块“文件不是模块错误”

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

typescript ES6:模块