美文网首页
TypeScript 学习笔记5

TypeScript 学习笔记5

作者: 躺希腊额阿毛 | 来源:发表于2019-08-29 18:09 被阅读0次

    export = 和 import = require()

    export =语法定义一个模块的导出对象。 它可以是类,接口,命名空间,函数或枚举。

    ZipCodeValidator.ts

    let numberRegexp = /^[0-9]+$/;
    class ZipCodeValidator {
        isAcceptable(s: string) {
            return s.length === 5 && 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" }`);
    });
    

    相关文章

      网友评论

          本文标题:TypeScript 学习笔记5

          本文链接:https://www.haomeiwen.com/subject/fpradctx.html