Skip to content

Suggestion: Implicit interfaces #4633

Closed
@b-strauss

Description

@b-strauss

Caveat: I have mainly written dart code during the last 12 months.

I think it would be great if a class would implicitly define an interface that you could implement (like in dart). The implicit interface would only contain the public instance members of the class and of any interfaces it implements.

This would help in cases where you don't have control over a class, it exists in some third party library, and they haven’t exposed an explicit interface.

Example:

class Person
{
    // Not in the interface, since this is private
    private name;

    // Not in the interface, since this is a constructor.
    constructor(name : string)
    {
        this.name = name;
    }

    // In the interface.
    greet(who : string) : string
    {
        return `Hello, ${who}. I am ${this.name}.`;
    }
}

// An implementation of the Person interface.
class Imposter implements Person
{
    greet(who : string) : string
    {
        return `Hello, ${who}. Do you know who I am?`;
    }
}

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