Struggling to develop a versatile function that returns the d3 scale
, encountering an issue where it's recognizing the wrong type following the switch
statement.
import * as D3Scale from 'd3-scale';
enum scaleIdentites {
linear,
time,
}
interface ScaleProps {
scaleIdentity: scaleIdentites;
range: number[];
domain: number[];
}
export const scale = ({ scaleIdentity, domain }: ScaleProps) => {
let scaleFunction: D3Scale.ScaleLinear<number, number> | D3Scale.ScaleTime<number, number>;
switch (scaleIdentity) {
case scaleIdentites.linear:
scaleFunction = D3Scale.scaleLinear();
scaleFunction.domain([1, 2]); // correctly reads the correct type and doesn't error.
break;
case scaleIdentites.time:
scaleFunction = D3Scale.scaleTime();
scaleFunction.domain([1, 2]); // correctly reads the correct type and doesn't error.
break;
default: {
throw new Error(`Unknown scale ${scaleIdentity}`);
}
}
if (domain) {
scaleFunction.domain(domain); // error indicating should have 0 parameters.
}
};
Successfully utilizing a parameter in domain
within the case
block, but encountering errors outside of it.