Skip to content

Color: Fix method return types to this#2144

Merged
Methuselah96 merged 1 commit intothree-types:masterfrom
eunjiyun:fix-color-return-types
May 9, 2026
Merged

Color: Fix method return types to this#2144
Methuselah96 merged 1 commit intothree-types:masterfrom
eunjiyun:fix-color-return-types

Conversation

@eunjiyun
Copy link
Copy Markdown
Contributor

@eunjiyun eunjiyun commented May 7, 2026

Summary

Fix Color method return types from Color to this for correct subclass chaining.

Evidence

JS implementation:

  • three.js/src/math/Color.js:187–525setScalar, setHex, setRGB, setHSL, setStyle, setColorName, copySRGBToLinear, copyLinearToSRGB, convertSRGBToLinear, convertLinearToSRGB all end with return this

Declaration:

  • types/three/src/math/Color.d.ts:233–298 — all of the above are declared as returning Color instead of this

Observed mismatch:

  • Return type is declared as Color, but the JS implementation returns this.
  • Declaring Color instead of this loses the subclass type when chaining. For example, if a user extends Color and chains one of these methods, TypeScript narrows the type to Color, making subclass-specific methods inaccessible:
  const exampleColor = new Color4(1, 0, 0, 0.5);

  exampleColor.a = 0.8;                    // direct access works
  exampleColor.setRGB(1, 1, 1).a = 0.8;    // Property 'a' does not exist on type 'Color'
                                 //    (setRGB is declared to return Color, not `this`)
  • Other methods in the same file (set, copy, add, lerp, etc.) already correctly use this. These methods were simply missed.

Local Check

bash pnpm test # failed: unrelated ESLint plugin error ​

Scope

  • Declaration file only
  • No runtime change
  • All changes isolated to Color.d.ts

@Methuselah96 Methuselah96 merged commit c526380 into three-types:master May 9, 2026
11 of 12 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.

2 participants