Algunos datos técnicos para empezar. tengo esta funcion:
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; } }recibe objeto de este tipo:
interface NotifyData { valueNumber?: number; valueString?: DirectionType | TypeOfSlider; valueBoolean?: boolean; valueArray?: Array<number>; target: TargetType; onlyState?: boolean; }entonces debería asignar algún valor al estado dependiendo del tipo de datos.objetivo
este.estado de este tipo:
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; }y algunos tipos de unión
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;Entonces el problema está aquí
this.state[data.target] = data.valueString;mecanografiado dice que "Escriba 'cadena | indefinido' no se puede asignar para escribir 'nunca'".
Si reemplazo el tipo de TargetType con una cadena, el error desaparece
UPD: gracias Alex Wayne por ejemplo