Generic Types Return in Typescript

Viewed 2622

I have a Interface find which consists of method cal(); Class a1 and a2 implements the interface. a1 returns a number while a2 returns string. How can i define a single interface to solve my issue.

The below is having the snippet of what is mentioned.

interface Ifind {
    cal() : string;
}

class a1 implements Ifind{

    public cal():string{
        return "10";
    }
}

class a2 implements Ifind{
    public cal(): number{
        return 12;
    }
}

class main{
    private obj;

    constructor() {
        this.obj = new a1();
        var p = this.obj.cal();
        alert(p)
    }
}

in TypeScript Playground

1 Answers
Related