Skip to content

ADTs with inheritance #20144

Description

I'd like to define an ADT and also enforce that all concrete types implement another interface (I know that is not how ADTs are supposed to be used, but since TypeScript is a mixed paradigm language, I'd like to be able to do it the way I'm used to it in Scala). This is an attempt to achieve it:

typeShape=Circle|RectangleinterfaceDrawable{draw();}classCircleimplementsDrawable{constructor(readonlykind: "circle",readonlyradius: number){}draw(){}}classRectangleimplementsDrawable{constructor(readonlykind: "square",readonlyweight: number,readonlyheight: number){}draw(){}}lets: Shape;s.draw();

The problem is that I cannot enforce that all concrete classes implement Drawable when defining them:

typeShape=Circle|Square|Triangle// This is ok, since there is no way to enforce Triangle to implement DrawableclassTriangle{constructor(readonlykind: "square",readonlyside1: number,readonlyside2: number,side3: number){}}lets: Shape;s.draw();// Error: property draw does not exist on Shape

and I only will get an error when actually call draw() on a Shape instance.

In Scala, for example, I can enforce that all concrete case classes to implement the Drawable interface:

traitDrawable {
defdraw():Unit
}
traitShapeextendsDrawablecaseclassCircle(radius: Int) extendsShape {
overridedefdraw() {}
}
caseclassSquare(width: Int, height: Int) extendsShape {
overridedefdraw() {}
}
// Error: Triangle does not implement drawcaseclassTriangle(side1: Int, side2: Int, side3: Int) extendsShape {
}

Is there a way to achieve this in TypeScript?
Is there a plan to allow discriminated union type matching feature with interfaces?
Is there a plan to allow type aliases to extend interfaces?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions