Imagine I have this simple TypeScript class, Animal.ts:
export default class Animal {
constructor(public name : string) { }
}
With this tsconfig.json file:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true
},
"files": [
"Animal"
]
}
How can I use the compiled version of this class (compiled by running tsc) in a javascript file like so:
var Animal = require("./Animal");
var newAnimal = new Animal();
Should I edit something in my tsconfig.json file? The error I get is:
ReferenceError: Animal is not defined
var Animal = require("./Animal").default<-- note the.defaultat the end.export defaultmakes your export an module. It would becomenew Animal.default('name'). However the error is not reproducible, the Animal is exported as{ __esModule: true, default: [Function: Animal] }.CRicesaid or omit thedefaultkeywordmodule.exports = .... You can doconst { Animal } = require('./animal');.