Repository navigation
refactor: convert the legend plugin to TypeScript - #12537
Merged
Merged
Conversation
|
Size Change: +3.85 kB (+1.05%) Total Size: 370 kB 📦 View Changed
ℹ️ View Unchanged
|
✅ Deploy Preview for chartjs-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
kurkle
force-pushed
the
rework-legend-p19-legend-ts
branch
from
November 26, 2026 21:08
8e288f7 to
4a58924
Compare
Rename src/plugins/plugin.legend.js to plugin.legend.ts and type it: the class fields are declared, the methods and helper functions have typed signatures, and the internal shapes (a hit box, a column size and the options as the plugin reads them once resolved) have types in the file. The public types stay in src/types/index.d.ts. The plugin object's hooks are left untyped. No behaviour changes. The emitted JavaScript differs only by the bare class field declarations and the default onClick's unused `e`, now `_e`. The layout item typedef in core.layouts.js typed `position` as a string; it is a LayoutPosition, which also allows the object form. etc/chart.js.api.md is regenerated: the legend plugin object is now declared as a const of an object type rather than as namespaces, with the same members, and the private Legend class it exposes as `_element` (Legend_2) is now recorded with its internal types.
core.layouts.js kept its own JSDoc description of LayoutItem next to the
public interface in src/types/layout.d.ts. It now imports that interface,
so there is one source for the shape.
buildLabels() read `this.options.labels || {}`. The rest of the class
reads `options.labels` unguarded, and the defaults always supply it, so
the fallback (and the cast it needed) is gone.
The P19 row of the rework-legend design lists core.layouts.js.
kurkle
force-pushed
the
rework-legend-p19-legend-ts
branch
from
November 26, 2026 21:11
4a58924 to
a751db9
Compare
kurkle
marked this pull request as ready for review
November 26, 2026 21:16
etimberg
approved these changes
Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The legend plugin was one of the last JavaScript plugins. Every later change to the legend gets type-checked if the plugin is written in TypeScript first. This PR converts it without changing behaviour.
What changed
src/plugins/plugin.legend.jsis nowsrc/plugins/plugin.legend.ts. The rename is in the same commit as the annotations, so git records it as a rename. Imports keep the.jsextension.LegendHitBox), a column size, and the options as the plugin reads them once they are resolved (LegendOpts: fonts are objects, a title can have several lines). The public types (LegendItem,LegendOptions,LegendElement) stay insrc/types/index.d.ts. The plugin object's hooks are left untyped.src/core/core.layouts.js(JSDoc only):Legend implements LayoutItemfromsrc/types/layout.d.ts, so itspositionis aLayoutPosition. The file's own JSDoc typedef ofLayoutItemsaidposition: string, solayouts.configureandlayouts.addBoxrejected the legend. The local typedef is replaced by@typedef {import('../types/layout.js').LayoutItem} LayoutItem, which leaves the public interface as the only description of a layout item.buildLabels()no longer falls back to{}whenoptions.labelsis missing. The defaults always supply it, and the other methods already read it without a fallback.onClick's unusedeparameter is renamed_e(noUnusedParameters).restoreTextDirection(ctx, opts.textDirection)gets a@ts-expect-error. The helper expects the[value, priority]pair thatoverrideTextDirectionsaved, and the legend passes the direction string. That mismatch is older than this PR and the call is left as it is. The tooltip makes the same call.etc/chart.js.api.mdis regenerated. Only the declaration form changes:.jsfile, tsc emitted the plugin object (plugins.Legend) as namespaces. It is now aconstof an object type, with the same members and the sameanytypes.@privateLegendclass, exposed as_element, now appears in the report asLegend_2with its typed members. The internal types it refers to (LegendOpts,LegendHitBox, ...) appear with it.Evidence
On master, the plain rename alone gives:
tsc --noEmit: 127 errors insrc/plugins/plugin.legend.ts: 124 × TS2339 (fields not declared) and 3 × TS2345 (clipArea(ctx, this), and twiceLegendnot assignable toLayoutItem).pnpm lint-ts-flags:128 diagnostic(s) in src/**/*.ts with the transition flags.(the same 127 plus TS6133 for the unusede).With this PR:
tsc --noEmit: 0 errors.pnpm lint-ts-flags: exits 0.src/**/*.{js,ts}: no errors. The file has one warning,adjustHitBoxesmax-statements (33), which it also has on master.test/specs/plugin.legend.spec.jsand theplugin.legendimage fixtures pass unchanged in Chromium and Firefox (136 tests).dist/chart.jsbuilt before and after: with comments ignored, the legend code differs only by the bare class field declarations (_added; legendHitBoxes; _hoveredItem; ..., in constructor order),onClick(_e, ...)and the dropped|| {}inbuildLabels(). The constructor still assigns every field. Rollup's cleanup step strips comments only from.jssources, so the legend's comments now stay in the unminified bundle, as they already do for the other.tsmodules.pnpm lint-types: thechart.jsreport matches the regeneratedetc/chart.js.api.md. The command still fails on master's staleetc/chart.js-helpers.api.md(_isClickEvent), which test: record the events parameter of _isClickEvent in the helpers API #12532 fixes. This PR does not touch that report.Try it
Open PRs that touch the legend
#11825 and #12242 edit
plugin.legend.js. Please rebase them onto this PR once it lands. The code is the same, only now inplugin.legend.ts.