Typescript: how to import a class from a javascript file?
There is no export default class
in JavaScript. What you can do is write your JS file like this. myClass/index.js
"use strict";
class MyClass {
hello(name) {
console.log(`Hello ${name}`);
}
}
exports.default = MyClass;
Create a Type definitions for it. myClass/index.d.ts
export default class MyClass {
hello(name: string): void;
}
You can then import it into your TypeScript like this.
/// <reference path="./myClass/index.d.ts" />
import MyClass from "./myClass";
const my = new MyClass();
my.hello("Stack Overflow");
at the end of the your javascript file , write this
exports.MyClass = MyClass;
in ts files
import * as IzendaSynergy from './izenda/izenda_ui.js';
or
import { IzendaSynergy } from './izenda/izenda_ui.js';
in tsconfig.json file
"allowJs": true