Typescript: Why doesn't TypeScript provide a way to implement an interface on the static side?

Created on 1 Aug 2017  ·  4Comments  ·  Source: microsoft/TypeScript

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code

I'm new to typescript and I'm wondering why not typescript add the class shape interface likes example below?

interface InstanceInterface {
    foo: string;
    bar(): string;
}

interface ClassInterface {
    // the constructor shape
    new (foo: string): InstanceInterface;
    // default static method
    baz(): void {
      console.log('I come from interface.')
    }
}

// We get hints guiding us to implement the constructor and static properties and static methods.
class Cls: ClassInterface implements InstanceInterface {
    constructor(foo: string) {
        this.foo = foo
    }
    static baz(): void {
        console.log('I override the interface baz.')
    }
    foo: string
    bar(): string {
        return this.foo
    }
}

As I know, Java8 added the static method defining and default implements.

Duplicate

Most helpful comment

Related: #1263, #13462.
Generally there's no need to declare that a class implements a static interface -- you can just pass it to a location expecting that interface and it will work even without a declaration. E.g.:

class C {
    static x() { return 0; }
}

function f(cls: { x(): number }) {
    return cls.x();
}

f(C); // Works!

All 4 comments

@zheeeng from a type checking point of view, you absolutely can do exactly what you want and with almost identical syntax:

export type Cls = typeof Cls;
export const Cls: ClassInterface = class implements InstanceInterface {
  constructor(public foo: string) {}
  static baz() {
    console.log('I override the interface baz.');
  }
  bar() {
    return this.foo;
  }
};

Also, comparing TypeScript to Java makes no more sense than comparing JavaScript to Java.

@aluanhaddad Thx, it works and is elegant!
A more question, is it being suggested to use in our programs? I don't know whether should we focus on shaping class in programming. If we should, I didn't find any practice writing like that and there are no the official guidelines or a convenient syntax likes I proposed. If I'm losing in confusions... I wish an explanation on why we don't need to check class shape interface.

Related: #1263, #13462.
Generally there's no need to declare that a class implements a static interface -- you can just pass it to a location expecting that interface and it will work even without a declaration. E.g.:

class C {
    static x() { return 0; }
}

function f(cls: { x(): number }) {
    return cls.x();
}

f(C); // Works!

@andy-ms @RyanCavanaugh What if libs like React requires that developer must manually set the static property displayname on each class component? The typescript friendly hints will help a lot.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CyrusNajmabadi picture CyrusNajmabadi  ·  3Comments

kyasbal-1994 picture kyasbal-1994  ·  3Comments

siddjain picture siddjain  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

Antony-Jones picture Antony-Jones  ·  3Comments