inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.16k stars 713 forks source link

Revisiting import "reflect-metadata" #1189

Open rweads0520 opened 4 years ago

rweads0520 commented 4 years ago

Different aspects of this have been covered in many forums, but I'd like to revisit to get some clarity and current thoughts. Is there no way the library can be updated to avoid this requirement? I don't see this requirement in other libraries that use TypeScript decorators. In its current state, it appears that even using modern javascript engines, e.g. Node.js 12+, and the documented additions to tsconfig.json, I still have to have import "reflect-metadata" at least once somewhere in the app. If it was only in the main app entry points that would be manageable, but in the case of unit tests that means in every spec file as well.

If you forget, it causes no compile-time errors, only run-time errors. So IDE's that make it easier to import required modules and highlight errors don't know anything about it. You just have to remember to add this seemingly arbitrary import every time or things don't work.

Great library, just this one issue adds a little confusion getting it to work.

yuric18 commented 1 year ago

Hi, @rweads0520!

Great points mentioned. Looking forward to a newer version that solves this issue (even 3 years after this issue). For now, all that I recommend is to have a setup file and import reflect-metadata on it. This way it's not needed to import it on every single test file.

// .vitest/setup.ts
import 'reflect-metadata'

// vitest.config.ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    // ...
    setupFiles: ['.vitest/setup.ts']
  },
})