Skip to content

this type should return instance type when overridden #2904

@mattjohnsonpint

Description

@mattjohnsonpint

Bug description

Found via StackOverflow: https://stackoverflow.com/q/79232050/634824

TypeScript allows the this keyword to be used as a type.

AssemblyScript seems to recognize this syntax and compiles fine:

class X {
  doSomething(): this {
      return this;
  }
}

However, in AssemblyScript, it always is treated as the class where this is used - which is not necessarily the type of the instance when the object is extended.

class Y extends X {
  doSomethingElse(): string {
      return "You made it!";
  }
}

Steps to reproduce

With the above code,

console.log(new Y().doSomething().doSomethingElse());

fails to compile. The error is:

ERROR TS2339: Property 'doSomethingElse' does not exist on type 'assembly/index/X'.
    :
 12 │ console.log(new Y().doSomething().doSomethingElse());
    │                                   ~~~~~~~~~~~~~~~
    └─ in assembly/index.ts(12,35)

Essentially, y.doSomething() is returning a type X where it should return a type Y.

TypeScript tooling believes the value should be Y, but AssemblyScript thinks it's X.

Image

Casting the value using <Y> or as Y works, but the point is that one may have full awareness of the original type when making the call.

AssemblyScript version

v0.27.32

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy