I am working with a class called Colors:
export class Colors {
constructor(
private domainColors: string[] = ['#F44336', '#FDB856', '#59CA08', '#08821C'],
private numberRange: [number | string, number | string] = [-100, 100],
private separators: number[] | string[] = [50, 62.5, 75]
) {
if (typeof numberRange[0] === 'string') {
numberRange[0] = parseInt(numberRange[0]);
}
if (typeof numberRange[1] === 'string') {
numberRange[1] = parseInt(numberRange[1]);
}
}
public adjustValueBy100(val: number): number {
const maxAllowedRange = this.numberRange[1] - this.numberRange[0];
return ((val - this.numberRange[0]) / maxAllowedRange) * 100;
}
}
Even though the constructor ensures that the numberRange
parameter is of type [number, number]
, when using it in the adjustValueBy100
function, the compiler shows an error indicating that numberRange
is still considered to be of type
[number | string, number | string]
.