I want to create an interface like this:
interface Show {
show(): string;
}
function doit(s: Show) {
return 'Showed: ' + s.show();
}
Then we can use it with a new class:
class Foo {
s: string;
constructor(s: string) {
this.s = s;
}
show() {
return 'Foo with "' + this.s + '"';
}
}
console.log(doit(new Foo('hello')));
I'd like to do the same thing for Number
s. In plain JavaScript I could make the type Number
, for example, satisfy this interface with:
Number.prototype.show = function() {
return '' + this;
}
But TypeScript doesn't let me do this:
show.ts(18,18): error TS2094: The property 'show' does not exist on value of type 'Number'.
Is there a way to do this?