Skip to content

fix: getColorPalette indexing #93699

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

Merged
merged 6 commits into from
Jun 17, 2025
Merged

fix: getColorPalette indexing #93699

merged 6 commits into from
Jun 17, 2025

Conversation

TkDodo
Copy link
Contributor

@TkDodo TkDodo commented Jun 17, 2025

The getColorPalette function was returning one too many elements: If we invoke getColorPalette(2), it returned an array with 4 elements.

The desired behaviour is for it to return 3 elements when invoked with getColorPalette(2), because we’d want the indices 0,1,2 to be available on it.

@github-actions github-actions bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Jun 17, 2025
Comment on lines -383 to -393
type NextTuple<T extends unknown[], A extends unknown[] = []> = T extends [
infer _First,
...infer Rest,
]
? // eslint-disable-next-line @typescript-eslint/no-restricted-types
Record<A['length'], Rest extends [] ? never : Rest[0]> &
NextTuple<Rest, [...A, unknown]>
: Record<number, unknown>;

type NextMap = NextTuple<TupleOf<ColorLength>>;
type Next<R extends ValidLengthArgument> = NextMap[R];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

coincidentally, we can now simplify the types greatly because we don’t need to look at the “next” element, as this +1 logic is gone.


const colors = theme.chart.getColorPalette(2);

expectTypeOf(colors).toEqualTypeOf<readonly ['#444674', '#d6567f', '#f2b712']>();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’ve added a type test with the expect-type library. This is what vitest is using under the hood too and it’ll allow us to write more type-level tests in the future.

@TkDodo TkDodo marked this pull request as ready for review June 17, 2025 13:30
@TkDodo TkDodo requested review from a team as code owners June 17, 2025 13:30
@TkDodo TkDodo merged commit d32ab70 into master Jun 17, 2025
45 checks passed
@TkDodo TkDodo deleted the tkdodo/fix/getColorPalette branch June 17, 2025 17:29
andrewshie-sentry pushed a commit that referenced this pull request Jun 19, 2025
The `getColorPalette` function was returning one too many elements: If
we invoke `getColorPalette(2)`, it returned an array with 4 elements.

The desired behaviour is for it to return 3 elements when invoked with
`getColorPalette(2)`, because we’d want the indices 0,1,2 to be
available on it.
@github-actions github-actions bot locked and limited conversation to collaborators Jul 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Scope: Frontend Automatically applied to PRs that change frontend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants