-
Notifications
You must be signed in to change notification settings - Fork 121
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
feat(types): improve generic types in specs, and spec prop types #1421
Merged
Conversation
This file contains 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
nickofthyme
added
enhancement
New feature or request
:specs
Chart specifications related issue
API
Changes the external API types
:all
Applies to all chart types
labels
Oct 7, 2021
9 tasks
@elasticmachine merge upstream |
@markov00 can you review this one please 🙏🏼 |
9 tasks
rshen91
reviewed
Dec 15, 2021
rshen91
approved these changes
Dec 15, 2021
nickofthyme
pushed a commit
that referenced
this pull request
Jan 5, 2022
# [42.0.0](v41.0.1...v42.0.0) (2022-01-05) ### Bug Fixes * **flamegraph:** solve animation regression occurring with 6db2677 ([#1541](#1541)) ([5ec6037](5ec6037)), closes [#1540](#1540) * **heatmap:** render empty state ([#1532](#1532)) ([59002df](59002df)) * **waffle:** fix strange 0 text in legend item extra when label is 0 ([#1538](#1538)) ([72224b9](72224b9)) ### Features * **goal:** add valueFormatter for tooltip ([#1529](#1529)) ([8139973](8139973)) * **heatmap:** add axis titles ([#1503](#1503)) ([a87325d](a87325d)) * **types:** improve generic types in specs, and spec prop types ([#1421](#1421)) ([562929e](562929e)) ### BREAKING CHANGES * **types:** The `xAccessor` and `yAccessor` are now required on all xy chart specs. Stronger typing on `data` prop that may cause type errors when using untyped array (i.e. `const arr: never[] = []`). Other minor type changes related to spec types. * **heatmap:** The heatmap yAxisLabel.padding style type is changed from Pixel | Partial<Padding> to Pixels | Padding. The heatmap axis labels are now correctly subjected to padding calculations and it will result in a slightly different position of labels. Co-authored-by: Marco Vettorello <vettorello.marco@gmail.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
:all
Applies to all chart types
API
Changes the external API types
enhancement
New feature or request
:specs
Chart specifications related issue
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.
Summary
Adds improved generic type for
Datum
inBarSeries
,LineSeries
,AreaSeries
,BubbleSeries
,BarSeries
,HistogramBarSeries
,Partition
,Heatmap
,GroupBy
andLineAnnotation
specs.All spec components now export corresponding
___Props
types (i.e.BarSeriesProps
fromBarSeries
).BREAKING CHANGE
The
xAccessor
andyAccessor
props on all relevant specs have been removed in favor of explicit accessors derived from the genericDatum
type. Stronger typing ondata
prop that may cause type errors.Details
specComponentFactory
Arguments
overrides
- Object containing overrides extended from theSpec
type. These values are replaced on theSpec
and are never exposed in the public API of the components. The propschartType
andspecType
are required. All values listed in overrides are excluded from being used adefaults
.defaults
- Object containing default values extended from theSpec
. Values provided will be forced as optional on theSpec
regardless of the optionality of the type on the originalSpec
type. These values will only be used when their respective prop on the spec component is not defined, this is in the form of a spread operator.The
specComponentFactory
method has been refactored to automatically infer the types of the final component props given the spec type, the overridden props and the default props. An example of this is shown below.buildSFProps
Arguments
overrides
- SeespecComponentFactory
descriptiondefaults
- SeespecComponentFactory
descriptionThis method is used to extract the implicit typings for a given spec definition outside of the
specComponentFactory
method. But why? The way typescript allows react component props to be driven my generics requires a top level function component that defines the generic types and props that consume these generics. ThusspecComponentFactory
would not work to defined generics that are unique to eachSpec
.The
BuildProps
interface is used to define the props given theoverrides
anddefaults
. You will see that the final type includesoptionals
andrequires
props but the note says it's deprecated. I did this to warn against using these as values because these are simply used to share the inferred types as we do not know ahead of time what optional or required props will be passed but we can infer what their types are.useSpecFactory
Arguments
props
- The fully built out props including overrides, defaults, optionals and required values.This react hook is a the magic behind the spec parser. The logic within this hook used to be called by implementing the same logic inside the factory-created spec component. This was fine but this required calling
getConnect()(MySpecFactory)
everywhere it was used. At the timereact-redux
connect
was used to connect the spec component to the redux store using thepure
option.The used of
pure
option is no longer required asreact-redux
has since made pure the default behavior across the board, see code.With this change we can now use the
useDispatch
hook in place of theconnect
method for a simplified usage.Thus creating a spec factory component with implied generic types would look something like...
This setup, defining
buildProps
before the spec withunknown
for the generic, can conflict with types and you may need to define thebuildProps
within the spec component function to access the correctDatum
type. In such case, use theuseRef
hook to only call this once. Seepackages/charts/src/chart_types/heatmap/specs/heatmap.ts
for an example of this.Now the shape of the
data
prop is used as theDatum
type across theBarSeries
props.Screen.Recording.2021-10-07.at.07.29.12.PM.mp4
The generic type for these spec components are implicit by default but can be explicitly defined as well such as...
Limitations
xAccessor
andyAccessors
could be forced to be a key of thedata
but this would be overly restrictive based on current chart usage. Thus, for now this will remainnumber | string
in addition to thekeyof Datum
however the loose typing will not show thekeyof Datum
values in the type.generic types cannot be shared between siblings, as of my current understanding. Thus
Settings
and other props that currently useDatum as any
will remain.This does NOT allow for generic types to be used in the vast majority of the source logic because of the separation of the react components and the redux store. Thus the generics will still fallback to a datum type of
any
.Issues
Related to #1380 and #547
Checklist
:xy
,:partition
):interactions
,:axis
)closes #123
,fixes #123
)packages/charts/src/index.ts