Answer the question
In order to leave comments, you need to log in
What is the correct way to export/import TypeScript classes from npm package?
In my package written in TS, I use namespaces and put each class in a separate file. For example like this:
// src/MyProject/Foo.ts
namespace MyProject{
export class Foo{}
}
// src/MyProject/Bar.ts
namespace MyProject{
export class Bar{}
}
namespace AnotherProject {
export class Baz extends MyProject.Bar{}
}
Answer the question
In order to leave comments, you need to log in
Read about namespace here . The official documentation says that namespaces are not really needed when the code is modularized.
So just use export on modules and import on files that use those modules.
/* src/Animal.ts */
export default Animal {
constructor(){}
}
/* src/Cat.ts */
import Animal from './Animal';
export default Cat extends Animal {
constructor(){
super();
}
}
/* index.ts */
import Cat from 'src/Cat';
let kitty = new Cat();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question