In AssemblyScript, I'm trying to define an instance method in a base class that will return the instance it's called on. I can do this, but the return type does not seem to be resolved correctly.
Here's an example. I try this, which works in TypeScript:
class X {
doSomething(): this {
return this;
}
}
class Y extends X {
doSomethingElse(): string {
return "You made it!";
}
}
log(new Y().doSomething().doSomethingElse());
In AssemblyScript, it seems that the type of .doSomething()
is determined to be X
rather than Y
.
ERROR TS2339: Property 'doSomethingElse' does not exist on type '__main__/X'. : 23 │ log(new Y().doSomething().doSomethingElse()); │ ~~~~~~~~~~~~~~~ └─ in __main__.ts(23,27)
I am new to both TypeScript and AssemblyScript, and am wondering if maybe there are differences in how these languages resolve this
- if maybe I just need to use something besides this
- or if this might be some sort of bug or feature that I could/should request. I'm also sorry if there is an answer to this available - I've been trying to construct a good search for it, and it hasn't turned up anything useful. I also tried to see what might be happening by looking through the AssemblyScript codebase, but after initial attempts, I think that will take me a very long time, and so I wanted to ask for the benefit of anyone who could happen to be in my situation. I'm using the assemblyscript
npm package at version 0.27.30
, which is currently only one patch version behind the latest version.
My question is - is there a way to declare doSomething
, from my example, so that AssemblyScript knows it returns the same type as the instance it is called on? Is there a difference in how AssemblyScript handles references to this
, that could help explain this?
Your code is valid TypeScript, but indeed this seems to be either a bug or a deficiency in AssemblyScript.
I've created a GitHub issue to report the problem: https://github.com/AssemblyScript/assemblyscript/issues/2904