我只是想在typescript接口中聲明一個靜態屬性?我在這方面還沒有找到任何地方。如何在TypeScript接口中定義靜態屬性
interface myInterface {
static Name:string;
}
這可能嗎?
我只是想在typescript接口中聲明一個靜態屬性?我在這方面還沒有找到任何地方。如何在TypeScript接口中定義靜態屬性
interface myInterface {
static Name:string;
}
這可能嗎?
您無法在TypeScript中的接口上定義靜態屬性。
說你想改變Date
對象,而不是試圖增加的Date
的定義,你可以把它包起來,或簡單地創建你豐富的日期類做的東西,Date
沒有做。
class RichDate {
public static MinValue = new Date();
}
因爲日期是打字稿的接口,你不能用類使用extends
關鍵字,這是一個有點羞恥的擴展它,因爲這將是一個很好的解決方案,如果日期是一類。
如果你想延長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);
靜態屬性通常放置在(全局)構造爲對象,而「接口」關鍵字適用於對象的實例。
如果您正在使用TypeScript編寫該類,那麼前面給出的答案當然是正確的。它可以幫助別人知道,如果你要描述一個已經在其他地方實現的對象,那麼全球構造包括靜態屬性可以這樣聲明:
declare var myInterface : {
new(): Interface;
Name:string;
}
@鄧肯的解決方案上面的靜態類型作品指定new()
也與接口:
interface MyType {
instanceMethod();
}
interface MyTypeStatic {
new():MyType;
staticMethod();
}
如果你正在尋找定義一個靜態類(即。所有的方法/屬性是靜態的),你可以做這樣的事情:
interface MyStaticClassInterface {
foo():string;
}
var myStaticClass:MyStaticClassInterface = {
foo() {
return 'bar';
}
};
在這種情況下,靜態的「類」實際上只是一個純ol'-JS對象,它實現的所有方法MyStaticClassInterface
可以正常定義接口:
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
}
你甚至可以保持類的名稱,打字稿(2.0)不會介意:
var MyClass: MyInterface;
MyClass = class MyClass {
static Name:string; // if the class doesn't have that field it won't compile
}
如果您wan't從多個接口繼承靜態你必須首先把它們合併成一個新問題:
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
}
使用相同的名稱工作example
我得到錯誤TS2322:匿名類不能分配到類型 – JoshuaDavid
@FireCoding您正在使用哪種TypeScript版本?我的例子使用2.0 –
您可以merge interface with namespace:
interface myInterface { }
namespace myInterface {
Name:string;
}
不過這個接口是隻有知道它有財產Name
有用。你不能實現它。
按照@ Duncan's @ Bartvds的回答,這裏提供了一個可行的方式,經過多年過去。
在打字稿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 open issue 13462。
你的答案是答案。非常感謝。使用Typescript 2.4.1 –
對於那些希望以一個靜態方法添加到內置對象,如日期,很容易
interface DateConstructor {
fromServerDate: any
}
現在Date.fromServerDate(appt.changeDate)
將編譯好的。
「DateConstructor」來自哪裏? – blockhead
我發現了一種方法來做到這一點(沒有裝飾)爲我的具體用例。
,檢查靜態成員的重要組成部分,是在createObject
方法IObjectClass
和使用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);
什麼是你想要的模型,到底是什麼? –