Skip to content

super should allow calling methods from merged interface #13563

@ghost

Description

TypeScript Version: 2.1.5

SUGGESTION: It seems to be no way to define extending for a class

Code

class Classic {
    
}

// some global object literal containing methods
declare window.somePOJO: { [key: string]: Function };
// dirty inheritance of POJO
Object.assign(Classic.prototype, window.somePOJO);

Question: How tot tell TS, that Classic is extended (has new methods)?

Attempt 1 -- Interface

class Classic ...
object.Assign(...

// declare inherited methods in interface of Classic
interface Classic {
    inheritedMethod(): void;
}

// but then in subClass
class SubClassic extends Classic {
    inheritedMethod() {
        super.inheritedMethod(); // Error TS2340´
        // Only public and protected methods of the base class are accessible via the 'super' 
    }
}

Attempt 2 -- Declare POJO as base class

// all methods of POJO in declared class
declare class somePOJO {
    public inheritedMethod(): void;
}

class Classic extends somePOJO {
    constructor() {
        // Error TS17009
        // 'super' must be called before accessing 'this' in the constructor of a derived class.
        // But super on POJO fails
    }
}

I searched hard in documentation and asked on StackOverflow, but defining extensions for class seems to be currently unsupported.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions