Jeff Voss
Jeff Voss

Reputation: 3695

Passing class to constructor in typescript results in error

I have some nested services I'm trying to create in Typescript, and I can't seem to figure out the correct syntax.

Here is the basic example:

class Service {
    constructor() { }
}

class AnotherService {
    constructor(private service: Service) { }
}

class yetAnotherService {
    constructor(private service: AnotherService) {  }
}

let myService = new yetAnotherService(AnotherService);

The last line errors with

Argument of type 'typeof AnotherService' is not assignable to parameter of type 'AnotherService'.
  Property 'service' is missing in type 'typeof AnotherService'.

This could be a duplicate question but I couldn't find the answer anywhere.

Upvotes: 0

Views: 28

Answers (1)

basarat
basarat

Reputation: 275847

The annotation is wrong. The following

constructor(private service: AnotherService) {  }

Should be

constructor(private service: typeof AnotherService) {  }

More

You want a class type not an instance.

Upvotes: 1

Related Questions