[Typescript] export = and import = require()

https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require

 

When you export using `export = `

let numberRegexp = /^[0-9]+$/;
class ZipCodeValidator {
  isAcceptable(s: string) {
    return s.length === 5 && numberRegexp.test(s);
  }
}
export = ZipCodeValidator;

 

Then to import those module you have to do `import = require()`

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"}`
  );
});

 

posted @ 2022-10-25 14:36  Zhentiw  阅读(118)  评论(0)    收藏  举报