簡體   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