mercredi 13 septembre 2017

Cannot call extension method on base class typescript

So i have a base class called Report, and then i have another class that extends from Report called Datasheet.

I have wrote an extension for the Report class, and i would like to be able to call it when i have a Datasheet object.

Simple example:

export class Report {
    id: number;
    name: string;
}

export class Datasheet extends Report {
    description: string;
}

Here i have wrote a simple extension for the report class

import { Report } from './report';

export {};

declare module './report' {
    interface Report {
        hasDescription(): boolean;
    }
}

Report.prototype.hasName = function() {
    return this.name == null || this.name.length > 0;
};

So now i create a new Datasheet and i want to call the extension method .hasName(), which compiles fine in typescript, but when it is sent to the browser, i get an error saying

m.hasName is not a function

Example, i would to do this (Simple example)

const m = new Datasheet();
const check = m.hasName();

Why am i getting an error, when this compiles fine? I am using Typescript 2.4.2.

Any help would be appreciated!




Aucun commentaire:

Enregistrer un commentaire