2016-08-04 65 views
1

所以给什么类型代表打字稿中的一种类型?

class Foo { 
} 

interface TypeProvider() { 
    type(): ? ; 
} 

class Bar implements TypeProvider { 
    type(): ? { 
     return (Foo); 
    } 
} 

class Baz implements TypeProvider { 
    type(): ? { 
     return (Bar); 
    } 
} 

Foo是一类,但如果我从方法返回一个类,什么类型我分配方法的签名?

作为旁边是return (Foo)return Foo是一样的东西?如果他们不同我不确定我不想要后者。

回答

2

应该是Foo构造:

class Bar { 
    type(): { new(): Foo } { 
     return (Foo); 
    } 
} 

或者:

interface FooConstructor { 
    new(): Foo; 
} 

class Bar { 
    type(): FooConstructor { 
     return (Foo); 
    } 
} 
+0

也许作为一个还...是'返回Foo'从'回报(美孚)'有什么不同? (希望我没有不小心写错了问题) – xenoterracide

+1

从问题标题中,不是OP询问表示任何构造函数的类型吗? –

+1

有没有必要有括号,你可以'返回Foo'。 @JuanMendes我认为他的意思是这种特定的类型,只是写'type',这样在阅读标题时会更容易理解。在任何情况下,代表任何构造函数的类型都将是:'{new():any}'。 –