-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeInfo not known in SvelteKit project #187
Comments
Removing the dependency of Service2 in Service1 removes the bug. // src/service/service1.ts
import { singleton } from "tsyringe";
import type { Service2 } from "./service2";
@singleton()
export class Service1 {
// Causes no error when commented
// constructor(private service2: Service2) {}
} |
The
If I import Service2 in I also tried moving both services directly into import 'reflect-metadata'
import type { Handle } from "@sveltejs/kit"
import { container, singleton } from "tsyringe"
@singleton()
export class Service1 {
constructor(private service2: Service2) {}
}
@singleton()
export class Service2 {}
export const handle: Handle = async ({ request, resolve }) => {
console.debug(container)
// I would like to print out the typeInfo Map from the container at this point, but I can't figure out how
const service1 = container.resolve(Service1)
console.debug(service1)
const response = resolve(request)
return response
} While I can't directly confirm because of being unable to use a debugger with SvelteKit and being unable figure out how to print typeInfo, I am reasonably confident that the error is thrown from the DependencyContainer.construct function because the typeInfo Map, for some reason, does not contain a Service1 entry at this point. tsyringe/src/dependency-container.ts Lines 481 to 493 in 1a9c0bd
One of my suspicions for the cause of this bug is that Vite is not internally compiling the TypeScript with metadata for the constructor arguments. The supporting info I have for this is that the bug is not triggered when the constructor is commented out, and the following from the README:
Vite does not use Babel internally; I think the equivalent would be its usage of esbuild. I've read a bit into the docs of Vite and esbuild, but can't find any particular configuration options that seem related as far as I can tell |
I'm facing this issue with Create React App (both 4.x and 5.x). I have tried enabling the Does anyone know how to get this working with create react app? |
I am also facing this issue on a Vue3 app that uses Vite |
Same, it has been 2 days now without solution. |
Waited 2 days? I wasted 3+ days trying to debug this as thoroughly as I could and provided as much info as possible, and the maintainers of the project have given me zero response all these months. 😓 Needless to say I've given up on using Tsyringe and unfortunately have not found an alternative I like. |
The problem is the compiler used by Vite, it doesn't work with annotations. Unless you change the underlying compiler of your project you are better off switching to an annotation-free dependency injection library like https://brandi.js.org/ |
Lol
I gave up and started using inversify |
I've just encountered the same problem (or at least something very similar). After lots of debugging it seems that doing a type import (from your example) |
I've been facing a similar issue. |
It seems Microsoft abandoned this repository and no one maintains it, so I gave up using it 3 years ago when I first encountered the issue. Good luck if you decide to use it. |
I had the issue with svelte, and I found a solution if it can help : |
Describe the bug
When I try to resolve a service using the class I get the error
Error: TypeInfo not known for "Service1"
To Reproduce
I tried making a minimal Typescript project to reproduce this (with just a main.ts, service1.ts, and service2.ts), however, the bug did not occur in that case. I tried to get a debugger connected, but unfortunately since the project is running in Vite I cannot do so properly because Vite is not producing sourcemaps yet (vitejs/vite#3928). I'm at a loss of how to further debug.
I started a fresh SvelteKit project and reproduced the structure of my working project as minimally as possible, and was able to trigger the bug in this configuration.
npm init svelte@next reproduction && cd reproduction
npm i reflect-metadata tsyringe
in the new projecttsconfig.json
src/hooks.ts
,src/service/service1.ts
, andsrc/service/service2.ts
npm run dev
and open webpage to trigger errorThe following is the resulting error from trying to access the webpage:
Expected behavior
No error should occur and service should be resolved.
Version:
4.6.0
The text was updated successfully, but these errors were encountered: