繁体   English   中英

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

[英]Passing class to constructor in typescript results in error

我想在Typescript中创建一些嵌套服务,但似乎找不到正确的语法。

这是基本示例:

class Service {
    constructor() { }
}

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

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

let myService = new yetAnotherService(AnotherService);

最后一行错误

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

这可能是一个重复的问题,但我在任何地方都找不到答案。

注释错误。 下列

constructor(private service: AnotherService) {  }

应该

constructor(private service: typeof AnotherService) {  }

更多

您要一个类类型而不是一个实例。

暂无
暂无

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

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