Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use this return type for Matrix4 in-place functions #705

Conversation

TimJentzsch
Copy link
Contributor

Objective

Closes #704.

Consider the following example:

export class Matrix4Ext extends Matrix4 {
  extension(): this {
    // -- Do stuff --
    return this;
  }
}

new Matrix4Ext()
  .multiply(new Matrix4())
  // Error: Property 'extension' does not exist on type 'Matrix4'.
  .extension();

This example doesn't compile, because the subclass type is erased by the Matrix4 return type of the .multiply function.

Solution

Instead of Matrix4, we now use this as return type for functions which return this.
I used the Matrix4 documentation on threejs.org to determine which return types should be this.

This allows type information of subclasses to be preserved through mutating functions.
Copy link
Contributor

@Methuselah96 Methuselah96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Methuselah96 Methuselah96 merged commit d24c099 into three-types:master Dec 12, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return this type instead of Matrix4 for in-place mutations
3 participants