[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 @   Zhentiw  阅读(112)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-10-25 [Functional Programming] Use the Callback and Closure Pattern to Build Advanced Async Behaviors [Full code]
2020-10-25 [Javascript] Broadcaster + Operator + Listener pattern -- 11. Customize the done logic
2020-10-25 [Javascript] Broadcaster + Operator + Listener pattern -- 10. Define a Function to Set Common Behaviors in Operators
2020-10-25 [Functional Programming] Start With the API You Want Then Implement
2020-10-25 [Javascript] Broadcaster + Operator + Listener pattern -- 9. Create modify, filter, map operators
2019-10-25 [Schematics] 1. Copy and Manipulate Template
2019-10-25 [Schematics] 0. Schematics "Hello World"
点击右上角即可分享
微信分享提示