![](/img/trans.png)
[英]In TypeScript, How to define type for Class that has a static method as arguments?
[英]How to define static class type for a variable in TypeScript?
我有一個類似於以下代碼的Angular2組件:
export ABC_Component class{
...
private xyzObj:any;
...
constructor(){
this.xyzObj= new ABC_Component.XYZ_Class();
}
...
private static XYZ_Class = class{
...
}
...
}
如您所見, xyzObj
屬於any
類型。 (在代碼的整體工作中沒有問題)
有沒有辦法正確定義它的類型,因為如果我嘗試定義它如下所示: private xyzObj:ABC_Component.XYZ_Class;
TypeScript Linter顯示Cannot find namespace for ABC_Component
Angular2和TypeScript都是新手。 任何幫助贊賞。
我真的不明白你想要實現的目標。 我試圖在靜態類屬性中定義類,這看起來很奇怪。
在任何情況下,您都沒有正確編寫此類:
export class ABC_Component {
private xyzObj:any;
constructor(){
this.xyzObj = new ABC_Component.XYZ_Class();
}
private static XYZ_Class = class {
}
}
這是另一種方法,如果您只想創建一個不會導出到其他人的類,並且只在此處使用它:
export class ABC_Component {
private xyzObj: XYZ_Class;
constructor(){
this.xyzObj = new XYZ_Class();
}
}
class XYZ_Class {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.