0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But now I cannot call any methods on the service from within the class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.