thiagobustamante / typescript-ioc

A Lightweight annotation-based dependency injection container for typescript.
MIT License
526 stars 64 forks source link

Class constructor PersonRestProxy cannot be invoked without 'new' #21

Closed terehov closed 6 years ago

terehov commented 6 years ago

Hi! I would really like to use your library, but I ran into weird errors like this one. Can you maybe give me a hint? thx!

import { Singleton, AutoWired, Inject, Container } from 'typescript-ioc';
@Singleton
@AutoWired
class PersonRestProxy {
  public random;
  constructor () {
    this.random = Math.random();
  }
}

class PersonDAO {
  @Inject
  public creationTime: Date;

  @Inject
  public restProxy: PersonRestProxy;
}

class PersonDAO3 {
  @Inject
  public creationTime: Date;

  @Inject
  public restProxy: PersonRestProxy = new PersonRestProxy();
}

let personDAO: PersonDAO = new PersonDAO();
console.log('**1', personDAO.creationTime, personDAO.restProxy.random);

let personDAO2: PersonDAO = new PersonDAO();
console.log('**2', personDAO.creationTime,  personDAO2.restProxy.random);

let personDAO3: PersonDAO3 = new PersonDAO3();
console.log('**3', personDAO3.creationTime,  personDAO3.restProxy.random);
thiagobustamante commented 6 years ago

Hi,

Can you check if you are using ES6 as output for your code (check tsconfig.json file)?

If you are using ES6, you must follow this instructions.

terehov commented 6 years ago

Thank you for your quick reply. I do follow this instructions. I am using it with ts-node, without “compilation”

terehov commented 6 years ago

Ok, I got it working. Somehow the anchor did not open previously. You were right, I had to add: import { Singleton, AutoWired, Inject, Container } from 'typescript-ioc/es6';

Thanks!