Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

75
Visualizações
Custom type for object indexing

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

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda