visx icon indicating copy to clipboard operation
visx copied to clipboard

`scaleThreshold` has incorrectly strict `domain` typing

Open ianstormtaylor opened this issue 3 years ago • 3 comments

For example for a threshold scale of:

const scale = scaleThreshold({
  domain: [1, 2, 5, 10],
  range: ['a', 'b', 'c', 'd']
})

It will infer a type of:

const scale: ScaleThreshold<1 | 2 | 5 | 10, string, never>

When I believe it should really be more generic:

const scale: ScaleThreshold<number, string, never>

ianstormtaylor avatar Feb 22 '21 21:02 ianstormtaylor

Hi @ianstormtaylor, thanks for reporting.

I was able to reproduce the incorrect inferred type here: https://codesandbox.io/s/competent-sun-0n5r4?file=/src/App.tsx

one workaround:

another:

const domain = [1, 2, 5, 10];
const range = ["a", "b", "c", "d"];

@kristw do you have any insights into TS failing to infer input type on inlined arguments?

hshoff avatar Feb 23 '21 15:02 hshoff

@kristw re-pinging you on this one

williaster avatar Apr 22 '21 20:04 williaster

I'm not sure about this too. Had the same issue and had to manually specify generic type (what @hshoff did) to overcome. Note that it only occurs for non-continuous scales. (linear, log, ... won't have this problem)

d3 is also not native ts but use type declaration so it could not leave the return type implicit. This sometimes make it too strict and could not rely on increasingly smarter inference.

kristw avatar Apr 22 '21 22:04 kristw