简体   繁体   English

在Typescript中将类传递给构造函数会导致错误

[英]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. 我想在Typescript中创建一些嵌套服务,但似乎找不到正确的语法。

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. 这可能是一个重复的问题,但我在任何地方都找不到答案。

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. 您要一个类类型而不是一个实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM