Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zheeeng opened this issue Aug 1, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@zheeeng
Copy link

zheeeng commented Aug 1, 2017

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.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Aug 1, 2017

@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.

@zheeeng
Copy link
Author

zheeeng commented Aug 1, 2017

@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.

@ghost
Copy link

ghost commented Aug 1, 2017

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!

@DanielRosenwasser DanielRosenwasser changed the title Why TypeScript doesn't provide a way to define the class shape interface? Why doesn't TypeScript provide a way to implement an interface on the static side? Aug 1, 2017
@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Aug 1, 2017
@zheeeng
Copy link
Author

zheeeng commented Aug 2, 2017

@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.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants