Skip to content

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

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

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

    Issue actions