Skip to content

Extension methods for interfaces #24889

Closed
@oliverjanik

Description

@oliverjanik

Apologies I this is a dup.

The situation comes up often. I define an interfaces for my API JSON responses and I wish they were classes so I could define instance methods on them. Say for mutation, or just helpful convenience methods.

Sure I can use standalone functions that take the interface as the first parameter but discover-ability sucks.

Something like this:

interface Rect {
    x: number;
    y: number;
    width: number;
    height: number;
}

function area(rect: this Rect): number {
    return rect.widht * rect.height;
}

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