[!WARNING] From 0.4.0, support for Node versions under v15.3.0 has been removed, as the package has transitioned to ES Modules.
See Why does the package not support Node versions under v15.3.0?.
[!NOTE] Experimental support for Bun has also been introduced. Feel free to test and report any issues!
Optional
, SkipSelf
, Self
, Many
and more, just like Angular!reflect-metadata
!)To start, you'll want to install the NPM package as part of your project.
$ npm install @freshgum/typedi
After that, you should be good to go. Read the documentation to get started!
import { Service, Container } from '@freshgum/typedi';
// Make a service that logs a message to the console.
@Service([])
class LogService {
log(message: string) {
console.log(message);
}
}
// Then, use our logging service in our root service,
// which will log "hello world!" to the console.
@Service([LogService])
class RootService {
// Store the instance of the logger inside the class.
constructor(private logger: LogService) {}
run() {
this.logger.log('hello world!');
}
}
// Now, run our service!
Container.get(RootService).run();
This DI implementation doesn't require any sort of precompilation / bundler steps, aside from the usual one provided by your TypeScript compiler.
Some goals of the project include:
Map
instances, with smarts for resolving dependencies, managing container hierarchies, resolving constraints, and some additional functionality.import
statements are used; this dramatically simplifies any required refactoring work, and general maintenance of consumer code.This container was initially made for my own use in code, though I will happily take suggestions, issues and feature requests to improve it.
These will be added if any features are requested that are not compatible with the project goals.
It's mainly a more modern, simpler version of the original TypeDI project, with more features, easier integration, and better error reporting. The naming isn't ideal, and it'll most likely be changed in the future[^5].
Yes. I regularly use it as part of my packages (and so do others!)[^6]. I didn't put in all this effort just to abandon the project. However, bear in mind that, as the goal of this package is to do one thing well, there may not be updates for periods if they are not explicitly required[^8], or if the addition of further functionality would go against the project goals.
However, I will happily review any MRs made against the source tree. If you wish to suggest a feature, I would prefer it if you could open an issue first to discuss whether the feature is appropriate, and whether its implementation is feasible.
Released under MIT by @freshgum & upstream TypeDI contributors.
[^1]: Tested on 23/11/2023. A lot of work is made to reduce the size of the bundle (a lot of work has also been inlined into other, non-related commits). Note that bundle size tests are performed by copying the minified typedi.min.mjs
file into ByteSizeMatters -- there are most likely better ways to test this. Investigation on reducing bundle size is then performed by formatting the minified file with Prettier, and assessing the bundle for unnecessary code / possible refactors; this is done iteratively until I am unable to find any further code size optimizations (which would not negatively affect performance / result in breaking changes). An example of a trick used to reduce the bundle size is name mangling: the Rollup configuration file contains code to minify certain members of internal classes (such as VisitorCollection
).
[^2]: No runtime dependencies are included. The only dependency of this package is type-fest (which only provides TypeScript types which are used internally). This dependency has been version-locked to ensure any breaches of that package's security does not impact anyone using this package. Any updates are checked and verified to ensure they do not contain malicious code.
[^3]: This mainly refers to the package's standard container-based interface, which makes testing easy (as you can replace services and values at any time). Further work is being done on a more featureful testing suite, which would be able to simplify the overall testing process.
[^4]: I haven't counted each one, but I'd say that the package exports ~40 types (as of writing: 23/11/2023); a lot of the safety is provided through typing, as opposed to unnecessary runtime checks, which affect performance and code size.
[^5]: In the future, I'll most likely look at renaming this package. That'll come naturally as part of a wider project. You'll probably notice that I avoid explicitly using this package's name in a lot of places; that will make it easier to update. The naming scheme is... unfortunate, and in retrospect I should have named it differently to avoid confusion with the original project.
[^6]: One example of such a project is ListenBrainz Discord RPC, which makes use of this package to structure its functionality into modular services. There are some other examples on GitHub's Dependents view, too.
[^7]: An example of "magic", in this context, would be integration with the filesystem to read a configuration file in a proprietary format, and then using that to configure services -- while that might make more sense for Java developers, such features don't (in my experience) scale well in JavaScript. Also, we'd have to write a ton of editor integrations! </ramble>
[^8]: If the library is ever feature-complete, it'll still be maintained -- compatibility with the latest engines will still be tested. However, as stated prior, features would not be added for the sake of adding features. Therefore, if this package ever becomes feature-complete (and is placed into maintenance mode), there's no need to ask if it's abandoned. If I ever become unable to continue maintaining the package, it shall be placed into archival (and the NPM package will become deprecated); in that case, please fork it and continue my efforts. All power to you!
[^9]: Tested via pnpm run build; cd build/bundles; for file in *.mjs; do printf "$file\t$(cat $file | wc -c | xargs printf "%'d\n")\t$(gzip -9c $file | wc -c | xargs printf "%'d\n")\t$(brotli -cZ $file | wc -c | xargs printf "%'d\n")\n"; done | tee
(credit: mrienstra on Stack Overflow)
Generated using TypeDoc