Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!