Skip to content

Question: can you define a class method outside the class brackets? #4950

@Zorgatone

Description

@Zorgatone

Hi,
I was wondering if it's possible in some way to define a class method outside of the class.
Right now I've used the "JS" prototype syntax to split the class definition from the method definitions like this:

class Foo {
    method: (arg: any) => any; // "prototype"-like
}

Foo.prototype.method = (arg: any): any => {
    // Method implementation [...]
}

But I was wondering if TypeScript could officially support this kind of separation with a better syntax (let me know if this is currently possible or planned for future versions). Like this:

Foo::method = (arg: any): any => {
    // Method implementation [...]
}

Or like this:

Foo::method(arg: any): any {
    // Method implementation [...]
}

If possible I'd really love to see this syntax supported in TypeScript.
This way we can write some class definitions first, without having to use an interface to describe each of those, and then implement the details of the methods (or constructor).

I guess this is related to the ES6 standard, I'm not sure if you would do something similar in EcmaScript.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions