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

Return this type instead of Matrix4 for in-place mutations #704

Closed
TimJentzsch opened this issue Dec 12, 2023 · 1 comment · Fixed by #705
Closed

Return this type instead of Matrix4 for in-place mutations #704

TimJentzsch opened this issue Dec 12, 2023 · 1 comment · Fixed by #705

Comments

@TimJentzsch
Copy link
Contributor

TimJentzsch commented Dec 12, 2023

Objective

I would like to create a subclass of Matrix4 to add my own custom methods while maintaining type safety.
However, when using functions like .multiply, the subclass is "lost", because it returns type Matrix4.
In reality, .multiply is an in-place operation which returns this, so the subclass is preserved.

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();

Solution

The type definitions should be adjusted to return this instead of Matrix4 if the operation is in-place and this is returned.
Which type definitions have to be adjusted can be inferred from the Three.js documentation on Matrix4 (using the this type).

@Methuselah96
Copy link
Contributor

Sure thing! Feel free to create a PR for this.

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 a pull request may close this issue.

2 participants