Having such a simple JS app:
add.ts:
function add(r:number){
return r + r;
}
exports.add = add;
app.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. 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
?
I've created the app using standard npm init -yes
and have installed @types/node
and typescript
itself (using Visual Studio Code as IDE).
Typescript Doc shows you how to use that when module
is 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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.