echarts: [Bug] tooltip.formatter option is not typed correctly
Version
5.4.2
Link to Minimal Reproduction
https://github.com/yay/echarts-tooltip-type-bug
Steps to Reproduce
Uncomment the following piece of code in main.ts in the linked GitHub repo to see the TypeScript error:
// tooltip: {
// formatter: (params) => {
// return `${params.name}, ${params.seriesName}`; // BUG: `name` and `seriesName` are unavailable because of incorrect typing
// },
// },
Current Behavior
Can’t access name, seriesName (and other props that are actually populated) in the params object passed to the tooltip formatter function because of incorrect typing echarts exposes.
Expected Behavior
Expected to access name, seriesName and other fields on the params object that are de facto there, but are missing in the declared type.
Environment
- OS: macOS 13.5
- Browser: Chrome 115.0.5790.170 (Official Build) (arm64)
- Framework: echarts 5.4.2 in a vanilla TypeScript app
Any additional comments?
No response
About this issue
- Original URL
- State: open
- Created 10 months ago
- Reactions: 1
- Comments: 15 (7 by maintainers)
But I saw the
axisLabelon the params object when I log it to console?It seems like
nameworks (and is the same asaxisLabel?) so if I useparams[0].nameand typecase the whole tooltip as you suggest it fixes the problem. Thank you@ChepteaCatalin Thanks! Yes, I’ve already switched to a slightly shorter type since I created this issue, which is similar to your suggestion:
@yay I suggest you use the following workaround, which appears less awkward:
paramsis expected to be of typeTooltipComponentFormatterCallbackParamswhich is an alias forTypeScript will try to infer the most general type from the union that encompasses both possibilities even if you want to infer only
CallbackDataParamstype. In your case,CallbackDataParams[]is inferred because it’s more general than justCallbackDataParams. So, you have to set explicitly that you wantCallbackDataParams, which is exported asDefaultLabelFormatterCallbackParams.As mentioned in the documentation,
paramscan take the form of either an object or an array so I don’t think that we should change anything in the library. @plainheart, what are your thoughts on this?