tsyringe: Uncaught TypeInfo not known for function Bar(foo)

I created a react app with --typescript install tsyringe and reflect-metadata and got this error

Uncaught TypeInfo not known for function Bar(foo)
import {container, injectable} from "tsyringe"

export class Foo {}

@injectable()
export class Bar {
  constructor(public foo: Foo) {}
}

export const myBar = container.resolve(Bar);

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 20 (3 by maintainers)

Most upvoted comments

If you stumble upon this issue outside of React, be sure that the service you’re resolving has a @injectable() decorator.

Do you have the following in your tsconfig.json?

"compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
}

I ended up walking through my “resolutions” until I found the ones, failing, they needed an @inject("tag") but, this is an especially terrible exception, because it doesn’t come from the attempt to resolve, and it doesn’t tell me what it can’t inject or what class it’s trying to inject to, or even which class it’s ultimately trying to resolve.

could we reopen this for better exceptions?

Anyone stumbling over this issue, check if you imported the class via import type. TypeScript does not catch, that the type is being used as a value here!

I had other types imported from a file when I auto-imported the class from it (which added it to the import type definition).

Maybe this specific problem could be solved with an eslint rule or something.

I opened a new issue #70

@Xapphire13 They support decorators for typescript https://github.com/facebook/create-react-app/releases/tag/v2.1.1

function foo() {
    console.log("f(): evaluated");
    return function (target:any , propertyKey: string, descriptor: PropertyDescriptor) {
        console.log("f(): called");
    }
}

class Test {
    @foo()
    method() {}
}

new Test().method()

Works just fine.