我只是想在typescript接口中声明一个静态属性?我没有找到任何关于这方面的资料。
interface myInterface {
static Name:string;
}
这可能吗?
我只是想在typescript接口中声明一个静态属性?我没有找到任何关于这方面的资料。
interface myInterface {
static Name:string;
}
这可能吗?
当前回答
如果您正在寻找定义一个静态类(即。所有的方法/属性都是静态的),你可以这样做:
interface MyStaticClassInterface {
foo():string;
}
var myStaticClass:MyStaticClassInterface = {
foo() {
return 'bar';
}
};
在这种情况下,静态“类”实际上只是一个普通的-ol'-js-object,它实现了MyStaticClassInterface的所有方法
其他回答
遵循@Duncan的@Bartvds的回答,在这里提供了一个可行的方法。
在Typescript 1.5发布后(@Jun 15 '15),你的有用界面
interface MyType {
instanceMethod();
}
interface MyTypeStatic {
new():MyType;
staticMethod();
}
可以在decorator的帮助下以这种方式实现。
/* class decorator */
function staticImplements<T>() {
return <U extends T>(constructor: U) => {constructor};
}
@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 issue 13462的评论。
视觉效果: 编译错误,提示缺少静态方法。
静态方法实现后,提示方法缺失。
在静态接口和正常接口完成后进行编译。
在TypeScript中,你不能在接口上定义静态属性。
假设您想要更改Date对象,而不是尝试添加到Date的定义中,您可以包装它,或者简单地创建富Date类来完成Date没有完成的工作。
class RichDate {
public static MinValue = new Date();
}
因为Date是TypeScript中的一个接口,你不能使用extends关键字将它扩展为一个类,这有点遗憾,因为如果Date是一个类,这将是一个很好的解决方案。
如果你想扩展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);
简单的例子
interface Person {
name: string;
age: number;
}
abstract class Trackable {
static TrackInstances: number;
}
class Pablo extends Trackable implements Person {
constructor(public name: string, public age: number) { Pablo.TrackInstances+=1; }
}
console.log(Pablo.TrackInstances);
这里没有提到的另一个选项是用表示静态接口的类型定义变量,并为其分配类表达式:
interface MyType {
instanceMethod(): void;
}
interface MyTypeStatic {
new(): MyType;
staticMethod(): void;
}
// ok
const MyTypeClass: MyTypeStatic = class MyTypeClass {
public static staticMethod() { }
instanceMethod() { }
}
// error: 'instanceMethod' is missing
const MyTypeClass1: MyTypeStatic = class MyTypeClass {
public static staticMethod() { }
}
// error: 'staticMethod' is missing
const MyTypeClass2: MyTypeStatic = class MyTypeClass {
instanceMethod() { }
}
效果与使用装饰器的应答相同,但没有装饰器的开销
操场上
GitHub上的相关建议/讨论
你可以正常定义接口:
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
}
为了表示一个类应该遵循这个接口来获取它的静态属性,你需要一点技巧:
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
}
如果你想静态地继承许多接口,你必须首先将它们合并到一个新的接口中:
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
}
工作示例