How to define a static property in the TypeScript interface

I just want to declare a static property in the typescript interface ? I did not find anything about this.

interface myInterface { static Name:string; } 

Is it possible?

+76
typescript
Dec 19
source share
11 answers

You cannot define a static property on an interface in TypeScript.

Suppose you want to modify a Date object, instead of trying to add Date definitions, you can wrap it or just create your own rich date class to make stuff that Date doesn't.

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

Since Date is an interface in TypeScript, you cannot extend it with a class using the extends , which is a bit shameful as it would be a good solution if the date was a class.

If you want to extend the Date object to provide the MinValue property on the prototype, you can:

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

Called with:

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

And if you want to make it available without an instance, you can also ... but it is a bit fussy.

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

Called with:

 var x: DateStatic = <any>Date; // We aren't using an instance console.log(x.MinValue); 
+34
Dec 19 '12 at 15:25
source share

Follow the @Duncan @Bartvds answer here to provide a workable path over the years.

At this point, after the release of Typescript 1.5 (@Jun 15 '15), your useful interface

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

can be implemented this way with a 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() {} } 

See my comment on the github 13462 public release .

visual result: compilation error with missing indication of a static method. enter image description here

After implementing the static method, there is no tooltip for the method. enter image description here

Compilation passed after the static interface and the normal interface are completed. enter image description here

+56
Apr 28 '17 at 7:24
source share

Static properties are usually placed in the (global) constructor for an object, while the "interface" keyword is applied to object instances.

The previous answer is, of course, correct if you are writing a class in TypeScript. This can help others find out that if you are describing an object that is already implemented elsewhere, a global constructor that includes static properties can be declared as follows:

 declare var myInterface : { new(): Interface; Name:string; } 
+13
Dec 20
source share

You can define the interface as usual:

 interface MyInterface { Name:string; } 

but you can't just do

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

To express that a class must follow this interface for its static properties, you need to be a little tricky:

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

You can even save the class name, TypeScript (2.0) will not mind:

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

If you do not want to inherit statically from many interfaces, you will have to merge them into a new one first:

 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 } 

Or, if you do not want to name a unified interface, you can do:

 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 } 

Working example

+12
Oct 12 '16 at 18:45
source share

@duncan's solution above by specifying new() for a static type also works with interfaces:

 interface MyType { instanceMethod(); } interface MyTypeStatic { new():MyType; staticMethod(); } 
+7
Sep 16 '13 at 21:00
source share

If you want to define a static class (i.e. all methods / properties are static), you can do something like this:

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

In this case, the static “class” is actually just a simple-ol'-js object that implements all the methods of MyStaticClassInterface

+3
Jan 11 '15 at 20:49
source share

You can combine the interface with a namespace with the same name:

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

But this interface is only useful to know that it has a Name property. You cannot implement it.

+2
Feb 17 '17 at 12:01
source share

I found a way to do this (without decorators) for my specific use case.

The important part that checks static elements is IObjectClass and using cls: IObjectClass<T> in the createObject method:

 //------------------------ // 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); 
+2
Jun 15 '17 at 18:01
source share

AFAIK, this is not possible.

Unfortunately, this is not good old Java.

+1
Oct 19 '18 at 22:27
source share

Yes it is possible. Here is the solution

 export interface Foo { test(): void; } export namespace Foo { export function statMethod(): void { console.log(2); } } 
+1
Apr 22 '19 at 7:22
source share

For those who want to add a static method to an inline object, such as Date, it's easy

 interface DateConstructor { fromServerDate: any } 

And now Date.fromServerDate(appt.changeDate) will compile fine.

0
May 28 '17 at a.m.
source share



All Articles