Currently, I am facing an issue with defining dependency injection in Typescript.
In my experience with JSDoc, I have used
typedef import('./classModule.js').default myClass
.
To illustrate, let's consider multiple classes stored in their own module files - classes A and B. In this scenario, we aim to specify that class B requires an instance of class A as a dependency in its constructor.
// A.js
export default class A {
//...
}
// B.js
// Using jsdoc, I can
/**
* @typedef {import ('./A').default} A
*/
export default class B {
/**
* @param {A} a
*/
constructor (a) {
this.a = a
}
}
What makes this interesting is that it's simply a comment. No actual import takes place.
But how can this be accomplished in Typescript?
From what I gather, I can utilize typeof A
for use as a type.
However, the challenge lies in having to import the class module for this, which is not ideal.
Instead, I would prefer to import an interface that has been generated from my class. This way, at runtime, there are no real dependencies or imports involved.