Skip to content

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

Closed
@zheeeng

Description

@zheeeng

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.

Activity

aluanhaddad

aluanhaddad commented on Aug 1, 2017

@aluanhaddad
Contributor

@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

zheeeng commented on Aug 1, 2017

@zheeeng
Author

@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
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?[/+] on Aug 1, 2017
zheeeng

zheeeng commented on Aug 2, 2017

@zheeeng
Author

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

locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @DanielRosenwasser@zheeeng@aluanhaddad@RyanCavanaugh

        Issue actions

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