microsoft / tsyringe

Lightweight dependency injection container for JavaScript/TypeScript
MIT License
5.18k stars 172 forks source link

Type info not know for `Foo` class #219

Closed ravisharmaa closed 1 year ago

ravisharmaa commented 1 year ago

Describe the bug

I was trying to set up tsyringe and play with it but seems I cannot make it work.

To Reproduce

import "reflect-metadata";
import { container } from "tsyringe";
import express, { Express, Request, Response } from 'express';
import { UserController } from './src/application/controller/UserController';

const app: Express = express();
const port = process.env.PORT || 3000;

app.get('/', (req: Request, res: Response) => {
  const user = container.resolve(UserController);
  const users = user.getUsers()
  console.log(users);
  res.send('Typing script = type script');
});

app.listen(port, () => {
  console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});

UserController.ts


import { autoInjectable } from "tsyringe";
import UserDto from "../../domain/auth/dto/UserDto";
import { UserService}  from "../../domain/auth/service/UserService";

autoInjectable()
export class UserController {
    constructor(public service?: UserService) {
        this.service = service;
    }

    getUsers(): string[] {
        return [
            "test"
        ];
    }

}

UserService.ts

import { autoInjectable } from "tsyringe";
import UserDto from "../dto/UserDto";
import { UserRepositoryInterface } from "../repository/UserRepositroyInterface";

@autoInjectable()
export class UserService {

    getAll(): UserDto[] {
        return [
            new UserDto('john doe', 21)
        ]
    }
}

tsconfig.json

{
    "compilerOptions": {
      "module": "commonjs",
      "esModuleInterop": true,
      "target": "es6",
      "noImplicitAny": true,
      "moduleResolution": "node",
      "types": ["reflect-metadata"],
      "sourceMap": true,
      "outDir": "dist",
      "baseUrl": "./src",
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "paths": {
        "*": ["node_modules/*"]
      }
    },
    "include": ["src/**/*"]
  }

package.json

{
  "name": "nomad-ts",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "scripts": {
    "build": "tsc",
    "start": "nodemon --inspect=0.0.0.0 --signal SIGINT index.ts"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "reflect-metadata": "^0.1.13",
    "ts-node": "^10.9.1",
    "tsyringe": "^4.7.0"
  },
  "devDependencies": {
    "@types/express": "^4.17.15",
    "@types/node": "^18.11.18",
    "typescript": "^4.9.4"
  },
  "nodemonConfig": {
    "watch": [
      "*.ts"
    ],
    "ext": "ts",
    "execMap": {
      "ts": "node --require ts-node/register"
    }
  }
}

Expected behavior

The controller should have been resolved. Version: 4.7.0

ravisharmaa commented 1 year ago

I also tried to use @injectable() in the UserService following this comment https://github.com/microsoft/tsyringe/issues/29#issuecomment-522880333 but no luck.

ravisharmaa commented 1 year ago

Seems like there is no activity on this package and as issues are not being addresses since Nov 2022.