Some technical info to start. I have this function:
updateState(data: NotifyData): void {
if (!data.onlyState) return;
if (typeof this.state[data.target] === 'string') {
this.state[data.target] = data.valueString;
} else if (typeof this.state[data.target] === 'number') {
this.state[data.target] = data.valueNumber;
} else {
this.state[data.target] = data.valueBoolean;
}
}
it recieves object of this type:
interface NotifyData {
valueNumber?: number;
valueString?: DirectionType | TypeOfSlider;
valueBoolean?: boolean;
valueArray?: Array<number>;
target: TargetType;
onlyState?: boolean;
}
then it should assign some value to state depending on the data.target type
this.state of this type:
interface State {
min: number;
max: number;
step: number;
direction: DirectionType;
type: TypeOfSlider;
valueFrom: number;
valueTo: number;
bubble: boolean;
onChangeTo: (value: number) => void;
onChangeFrom: (value: number) => void;
[key: string]: StateContent;
}
and some union types
type TargetType = 'valueTo' | 'valueFrom' | 'max' | 'min' | 'direction' | 'type' | 'bubble';
type DirectionType = 'horizontal' | 'vertical';
type TypeOfSlider = 'single' | 'double'
type StateContent = number | boolean | undefined | ((value: number) => void)
| DirectionType | TypeOfSlider;
So problem is here
this.state[data.target] = data.valueString;
typescript says, that "Type 'string | undefined' is not assignable to type 'never'".
If i replace type of TargetType with string then the error disappears
UPD: thanks Alex Wayne for example