I am very new to TypeScript and am trying to export a class for use in my JS code.
project/testmodule/index.ts
class Foo {
constructor(bar: number){
this.number = number;
};
bar: number;
}
project/testmodule/index.js
"use strict";
/// <reference path="index.ts" />
Object.defineProperty(exports, "__esModule", { value: true });
project/index.js (main file)
const { Foo } = require('./testmodule');
console.log(new Foo(1).bar.toString());
on run:
TypeError: Foo is not a constructor
at Object.<anonymous> (C:\Users\willi\OneDrive\Desktop\project\index.js:3:13)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
Is the error glaringly obvious? I don't understand what I did wrong here. VSCode is making this more confusing since it does seem to understand Foo as a constructor.
exportandrequireare not compatibleexport class Foo { … }to export it. Looks like the compiler even strips away the dead code and produces an empty module in the.jsfile.export, still have the same errorexport