InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

(lazy) Injection causes 'undefined'/'Cannot access '{InjectableService}' before initialization' error

Open marwalsch opened this issue 3 years ago • 6 comments

I need property injection for a React project. However, even the most basic example won't work and causes the injected Property to be undefined. I tried inversify-react-decorators but the configuration causes another, even more confusing error.

SomeService.ts

import { inject, injectable } from "inversify";
import { TYPES, ITokenProvider, IFileService } from "../injectables";
import { lazyInject, DIContainer } from "../inversify.config";

export class SomeService {

    @inject(TYPES.ITokenProvider) private tokenProvider!: ITokenProvider;
    //@lazyInject(TYPES.ITokenProvider) private tokenProvider!: ITokenProvider;   //either of these
     (...)
}

inversify.config.ts


import "reflect-metadata";
import { Container } from "inversify";
import getDecorators from "inversify-inject-decorators";

import { TYPES, ITokenProvider, } from "./injectables";
import { TokenProvider } from "./Services/TokenProvider"


const DIContainer = new Container();
DIContainer.bind<ITokenProvider>(TYPES.ITokenProvider).toConstructor(TokenProvider);


const { lazyInject } = getDecorators(DIContainer, false);

export { DIContainer, lazyInject }

injectables.ts

export interface ITokenProvider {
    getSomeToken(): Promise<string>
}

TokenProvider.ts

import "reflect-metadata";
import * as microsoftTeams from "@microsoft/teams-js";

import { injectable } from "inversify";
import { ITokenProvider } from '../injectables';

@injectable()
export class TokenProvider implements ITokenProvider {

    public constructor() { }

    public async getSomeToken(): Promise<string> {
        (...)
    }
}

Current Behavior

Neither Solution is working.

Using @inject causes

SomeService.ts:35 Uncaught (in promise) TypeError: Cannot read property 'getSomeToken' of undefined
   at
   (...)

Using @lazyInject causes

TokenProvider.ts:8 Uncaught ReferenceError: Cannot access 'SomeService' before initialization
    at Module.SomeService (VM8 main.chunk.js:248)
    at Module../src/inversify.config.ts (inversify.config.ts:11)
   (...)

Which is really mind boggling as I couldn't fin anything at all on this. Initializing "TokenService" in the constructor though an explicit call works as expected, so it doesn't seem to be an syntactical issue;

Environment

tsconfig.json

{
  "include": [
    "src/*"
  ],
  "compilerOptions": {
    "target": "es5",
    "jsx": "react",
    "allowSyntheticDefaultImports": true,
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "types": [ "reflect-metadata" ]
  }
}

Versions: inversify": "^5.0.5", inversify-inject-decorators": "^3.1.0",

marwalsch avatar Dec 14 '20 14:12 marwalsch