2012-12-19 76 views

回答

27

您無法在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); 
+1

@Rajagopal要清楚的是,您實際上可以使用'extends'關鍵字擴展TS中的接口。你不能用一個類擴展一個接口(爲了添加一個靜態屬性,你需要這樣做)。 – JcFx

+0

Steve - '因爲Date是TypeScript中的一個接口,所以不能使用extends關鍵字來擴展它 - - 這是不對的,是嗎? – JcFx

+1

您可以擴展接口,但不能用類擴展它,只能實現它。我沒有說清楚。 – Fenton

12

靜態屬性通常放置在(全局)構造爲對象,而「接口」關鍵字適用於對象的實例。

如果您正在使用TypeScript編寫該類,那麼前面給出的答案當然是正確的。它可以幫助別人知道,如果你要描述一個已經在其他地方實現的對象,那麼全球構造包括靜態屬性可以這樣聲明:

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

編輯:發佈作爲一個完整的答案[下面](http://stackoverflow.com/a/18837239/1026362) – Bartvds

+0

在這之後發佈Typescript 1.5(6.15.2015),這是一個可行的方式來描述靜態成員一類。 http://stackoverflow.com/a/43674389/681830 – Val

6

@鄧肯的解決方案上面的靜態類型作品指定new()也與接口:

interface MyType { 
    instanceMethod(); 
} 

interface MyTypeStatic { 
    new():MyType; 
    staticMethod(); 
} 
+0

其中哪一個會讓我的課程實現? –

+1

此時您不能使用接口來描述靜態成員,只有實例成員。所以在這個例子中,你的類將實現'MyType'(就像'Foo類實現MyType'一樣)。當描述現有的JS代碼時,靜態接口在定義中才是真正有用的。 – Bartvds

+0

在Typescript 1.5發佈(6.15.2015)之後的這一時刻,這裏描述一個類的靜態成員是一種可行的方法。 http://stackoverflow.com/a/43674389/681830 – Val

3

如果你正在尋找定義一個靜態類(即。所有的方法/屬性是靜態的),你可以做這樣的事情:

interface MyStaticClassInterface { 
    foo():string; 
} 

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

在這種情況下,靜態的「類」實際上只是一個純ol'-JS對象,它實現的所有方法MyStaticClassInterface

6

可以正常定義接口:

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

+0

我得到錯誤TS2322:匿名類不能分配到類型 – JoshuaDavid

+0

@FireCoding您正在使用哪種TypeScript版本?我的例子使用2.0 –

0

您可以merge interface with namespace

interface myInterface { } 

namespace myInterface { 
    Name:string; 
} 

不過這個接口是隻有知道它有財產Name有用。你不能實現它。

20

按照@ 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

可視化結果: 編譯錯誤,提示缺少靜態方法。 enter image description here

實現靜態方法後,提示爲方法失蹤。 enter image description here

靜態接口和普通接口都滿足後編譯通過。 enter image description here

+2

你的答案是答案。非常感謝。使用Typescript 2.4.1 –

0

對於那些希望以一個靜態方法添加到內置對象,如日期,很容易

interface DateConstructor { 
    fromServerDate: any 
} 

現在Date.fromServerDate(appt.changeDate)將編譯好的。

+0

「DateConstructor」來自哪裏? – blockhead

1

我發現了一種方法來做到這一點(沒有裝飾)爲我的具體用例。

,檢查靜態成員的重要組成部分,是在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); 
相關問題