Skip to content
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

Type info not know for Foo class #219

Closed
ravisharmaa opened this issue Jan 8, 2023 · 2 comments
Closed

Type info not know for Foo class #219

ravisharmaa opened this issue Jan 8, 2023 · 2 comments
Assignees

Comments

@ravisharmaa
Copy link

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
Copy link
Author

I also tried to use @injectable() in the UserService following this comment #29 (comment) but no luck.

@ravisharmaa
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants