Qvatra
Qvatra

Reputation: 3857

How to inherit from existing AMD js class in TypeScript compilng to AMD module?

I did try the following:

class Child {
    public func() {
       alert("hello");
    }
    constructor() {
        Parent.apply(this, arguments);
    }
}
Child["prototype"] = Object.create(Parent.prototype)
Child["prototype"].constructor = Child;

but the instance of the Child class doesn't have func() method.

How to do inheritance from js class?

Upvotes: 1

Views: 304

Answers (1)

Paleo
Paleo

Reputation: 23702

General case

Just use declare in order to declare an existing class.

declare class Parent { // produces no JS code
    constructor(val);
}
class Child extends Parent {
    constructor(val) {
        super(val);
    }
}

Case of an external module (AMD)

The external module has to be described in a definition file .d.ts.

File ContainerSurface.d.ts:

declare class ContainerSurface {
  constructor(options);
}
export = ContainerSurface;

How to use it:

import ContainerSurface = require('ContainerSurface');

class Child extends ContainerSurface {
  constructor(options) {
    super(options);
  }
}

Upvotes: 4

Related Questions