-
I'm using a pure JavaScript project with a jsconfig.json file. Due to the nature of my project, it does not bundle (no need for babel transpilation, or tsc). If the answer is yes, i'd love to see an example ❤️ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @talkohavy , Yes, any TypeScript package, including Inversify, can indeed run in JavaScript. While TypeScript offers additional capabilities that JavaScript lacks (like decorators), Inversify has you covered! The Inversify team anticipated this need, so they included a helper function called decorate that makes it easy to apply decorators in JavaScript. Here’s a simple example to get you started:
import "reflect-metadata";
import { Container, injectable, decorate } from "inversify";
class Warrior {
fight() {
return "Warrior fighting";
}
}
// Manually decorate the Warrior class as injectable
decorate(injectable(), Warrior);
const container = new Container();
container.bind(Warrior).toSelf();
const warrior = container.get(Warrior);
const resp = warrior.fight();
console.log(resp); // Warrior fighting
cc: @notaphplover |
Beta Was this translation helpful? Give feedback.
Hey @talkohavy ,
Yes, any TypeScript package, including Inversify, can indeed run in JavaScript. While TypeScript offers additional capabilities that JavaScript lacks (like decorators), Inversify has you covered!
The Inversify team anticipated this need, so they included a helper function called decorate that makes it easy to apply decorators in JavaScript.
Here’s a simple example to get you started:
main.js