如何在TypeScript接口中定义静态属性

我只想在typescript接口中声明静态属性 ? 我还没有find任何关于这个。

interface myInterface { static Name:string; } 

可能吗?

您不能在TypeScript中的接口上定义静态属性。

假设你想改变Date对象,而不是试图添加到Date的定义中,你可以把它包装起来,或者简单地创build你的丰富的date类去做Date不行的东西。

 class RichDate { public static MinValue = new Date(); } 

因为Date是TypeScript中的一个接口,所以不能使用extends关键字来扩展它,这有点令人遗憾,因为如果date是一个类,这将是一个很好的解决scheme。

如果要扩展Date对象以在原型上提供MinValue属性,则可以:

 interface Date { MinValue: Date; } Date.prototype.MinValue = new Date(0); 

调用使用:

 var x = new Date(); console.log(x.MinValue); 

如果你想在没有实例的情况下使用它,你也可以…但它有点挑剔。

 interface DateStatic extends Date { MinValue: Date; } Date['MinValue'] = new Date(0); 

调用使用:

 var x: DateStatic = <any>Date; // We aren't using an instance console.log(x.MinValue); 

按照@Duncan的@ Bartvds的回答,在这里提供一个可行的方式,经过多年的过去。

在Typescript 1.5发布(@Jun 15'15)之后的这一点,你的有用的接口

 interface MyType { instanceMethod(); } interface MyTypeStatic { new():MyType; staticMethod(); } 

可以用装饰者的帮助来实现这种方式。

 /* class decorator */ function staticImplements<T>() { return (constructor: T) => {} } @staticImplements<MyTypeStatic>() /* this statement implements both normal interface & static interface */ class MyTypeClass { /* implements MyType { */ /* so this become optional not required */ public static staticMethod() {} instanceMethod() {} } 

请参阅我在github的开放问题13462的评论。

可视化结果:编译错误,提示缺less静态方法。 在这里输入图像说明

在执行静态方法后,提示缺less方法。 在这里输入图像说明

在静态接口和普通接口都满足的情况下通过编译。 在这里输入图像说明

静态属性通常放置在对象的(全局)构造函数中,而“接口”关键字适用于对象的实例。

前面给出的答案当然是正确的,如果你在TypeScript中编写类。 它可以帮助别人知道,如果你正在描述一个已经在其他地方实现的对象,那么包含静态属性的全局构造函数可以这样声明:

 declare var myInterface : { new(): Interface; Name:string; } 

上面的@ duncan解决scheme指定静态types的new()也适用于接口:

 interface MyType { instanceMethod(); } interface MyTypeStatic { new():MyType; staticMethod(); } 

你可以正常定义接口:

 interface MyInterface { Name:string; } 

但是你不能这样做

 class MyClass implements MyInterface { static Name:string; // typescript won't care about this field Name:string; // and demand this one instead } 

为了expression一个类应该遵循这个接口的静态属性,你需要一些技巧:

 var MyClass: MyInterface; MyClass = class { static Name:string; // if the class doesn't have that field it won't compile } 

你甚至可以保留类的名字,TypeScript(2.0)不会介意:

 var MyClass: MyInterface; MyClass = class MyClass { static Name:string; // if the class doesn't have that field it won't compile } 

如果你不想从很多接口静态的inheritance,你必须先把它们合并成一个新的:

 interface NameInterface { Name:string; } interface AddressInterface { Address:string; } interface NameAndAddressInterface extends NameInterface, AddressInterface { } var MyClass: NameAndAddressInterface; MyClass = class MyClass { static Name:string; // if the class doesn't have that static field code won't compile static Address:string; // if the class doesn't have that static field code won't compile } 

或者,如果你不想命名合并的界面,你可以这样做:

 interface NameInterface { Name:string; } interface AddressInterface { Address:string; } var MyClass: NameInterface & AddressInterface; MyClass = class MyClass { static Name:string; // if the class doesn't have that static field code won't compile static Address:string; // if the class doesn't have that static field code won't compile } 

工作示例

如果你正在寻找定义一个静态类(即所有的方法/属性是静态的),你可以做这样的事情:

 interface MyStaticClassInterface { foo():string; } var myStaticClass:MyStaticClassInterface = { foo() { return 'bar'; } }; 

在这种情况下,静态“类”实际上只是一个普通的ol'-js对象,它实现了MyStaticClassInterface所有方法

您可以使用相同的名称将界面与命名空间合并 :

 interface myInterface { } namespace myInterface { Name:string; } 

但是这个接口只有知道它的属性Name是有用的。 你不能实现它。

对于那些希望添加一个静态方法到一个内置的对象,如date,这很容易

 interface DateConstructor { fromServerDate: any } 

现在Date.fromServerDate(appt.changeDate)会正常编译。

我发现了一个方法来做到这一点(没有装饰)为我的具体使用情况。

检查静态成员的重要部分是IObjectClass并在createObject方法中使用cls: IObjectClass<T>

 //------------------------ // Library //------------------------ interface IObject { id: number; } interface IObjectClass<T> { new(): T; table_name: string; } function createObject<T extends IObject>(cls: IObjectClass<T>, data:Partial<T>):T { let obj:T = (<any>Object).assign({}, data, { id: 1, table_name: cls.table_name, } ) return obj; } //------------------------ // Implementation //------------------------ export class User implements IObject { static table_name: string = 'user'; id: number; name: string; } //------------------------ // Application //------------------------ let user = createObject(User, {name: 'Jimmy'}); console.log(user.name);