[英]Type checking in commonJS modeles
Having such a simple JS app:拥有这样一个简单的 JS 应用程序:
add.ts:添加.ts:
function add(r:number){
return r + r;
}
exports.add = add;
app.ts :应用程序.ts :
var utils = require('./add.js');
console.log(utils.add(4));
I get no type checking error when passing in a string instead of a number:传入字符串而不是数字时,我没有收到类型检查错误:
...
console.log(utils.add("abc"));
There is no error info both in the IDE nor on the output of the npx tsc
command.在 npx
npx tsc
命令的 IDE 和 output 中都没有错误信息。 I know that using the ES6
syntax ( export...import
) type checking would work but my question is how to enable this checking in commonJS
standard with exports...require
?我知道使用
ES6
语法( export...import
)类型检查会起作用,但我的问题是如何使用 export exports...require
在commonJS
标准中启用这种检查?
I've created the app using standard npm init -yes
and have installed @types/node
and typescript
itself (using Visual Studio Code as IDE).我使用标准
npm init -yes
创建了应用程序,并安装了@types/node
和typescript
本身(使用 Visual Studio Code 作为 IDE)。
Typescript Doc shows you how to use that when module
is CommonJS
. Typescript Doc向您展示了当
module
是CommonJS
时如何使用它。
So in your snippets, do following changes.因此,在您的代码段中,请进行以下更改。
// app.ts
import add = require('./add.js');
// add.ts
function add(r:number){
return r + r;
}
export = add;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.