Try to write a generic function function to return d3 scale. but getting the following error as it is reading the wrong type after 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 doesnt error.
break;
case scaleIdentites.time:
scaleFunction = D3Scale.scaleTime();
scaleFunction.domain([1, 2]); // correctly reads the correct type and doesnt error.
break;
default: {
throw new Error(`Unknow scale ${scaleIdentity}`);
}
}
if (domain) {
scaleFunction.domain(domain); // error as saying should have 0 parameters.
}
};
When inside the case block it correctly allows me to use a parameter in domain. Outside it errors.
scaleTimeusesDateas domain elements. Or are you interested in the first ms after 1970-01-01. You assign the result of a lambda that does not return a result.domain: Array<number | Date>;but still get the same error. It seems liketypescriptkeeps linking back todomain(): number[]within thed3-scaletypes. When it should see it can havedomain(domain: Array<number | { valueOf(): number }>): this;also. Ive basically had to duplicate code in the respectivecasestatementssrc/app/Components/D3/utils/scale.ts:55:5 - error TS2554: Expected 0 arguments, but got 1.scale.ts::55?