fix bug (typescript) in creating log scales #2001
Merged
+233
−105
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.
Fixes a typescript-related bug in a function that creates log scales (package
@nivo/scales
)Edited: adjusts usage of scale-related ts types in packages @nivo/bar, @nivo/marimekko, @nivo/scatterplot, @nivo/stream
Context
The function
createLogScale
creates an object correctly. However, the way the object is returned at the end creates confusion for the TypeScript compiler, making it believe the object has an incorrect type.As an example, consider:
Currently, the compiler gives errors, stating that outputs from
createLogScale
and fromcomputeScale
are incompatible with the types on the left-hand-side. The outputs actually carry all the right content - its just that the compiler is confused. The fix gives typescript a clearer indication about the returned object.I tried to create unit tests that would yield an error before the fix and pass after the fix, but I couldn't manage. The reason is that this is problem appears only during builds of code like above. Putting such code in unit tests shows messages in an IDE, but executes without problems, regardless of the fix. To trigger an error, such code should be in the package src. However,
@nivo/scales
doesn't have/need such code in src. That pattern is found in other packages.Impact
The issue addressed by this fix is the likely cause why some nivo packages occasionally use castings like
xScale as any
. With this edit, such fragments could be replaced byxScale as AnyScale
or even plainxScale
.Affected packages are Bar, Marimekko, ScatterPlot, Stream. It could be relevant to Line once that is migrated to TypeScript (#1932). Swarmplot could be extended to support log scales, so this would be relevant there too.
I can edit those packages and remove ~24 linting warnings. If so, changes would be dispersed across several packages. What do you think?
Edited:
After discussion, scope expanded to update usage of typescript types for scale objects across packages.
@nivo/scatterplot
@nivo/bar
type.ts
. In those cases, I opted to include more fields in the layer props (not removing anything). Existing code that relies on these props should work as before.@nivo/marimekko
@nivo/stream
Some of the changes involve replacing
Scale<any, any>
byAnyScale
. The latter is defined in @nivo/scales and it is actually the same asScale<any, any>
. The advantage is thatAnyScale
can now be restricted, if needed, in only one place.Overall, the changes do not add external features and there should be no breaking changes. The number of warnings emited from
make packages-lint
decreased from 287 to 254.