I'm working on a class that implements a generic interface like:
interface World<T> { get(): T; }
class Hello implements World< Hello > { get(){ return this } }
But then I was thinking that what if another class than Hello
will implement World
interface, intead of having a to use World< AnotherClass >
I was thinking about something like World< World >
so it can be permanent wherever I implement the World
interface, but it looks like Typescript don't accept that syntax, is there any better way to do so?, maybe there is a way around it?
I'm sorry but it's not clear what you mean by "so it can be permanent wherever I implement the World interface". Maybe you just want a non-generic interface?
interface World {
get(): World;
}
class Hello implements World {
get() {
return this;
}
hello() {
console.log("hello");
}
}
class AnotherClass implements World {
get() {
return this;
}
anotherClass() {
console.log("another class");
}
}
var h = new Hello();
var ac = new AnotherClass();
var testH = h.get(); //testH is implicitly typed as Hello
var testAC = ac.get(); //testAC is implicitly typed as AnotherClass
//these all work.
var testWorld : World;
testWorld = h.get();
testWorld = ac.get();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments