S
S
Slav4ka2021-12-01 00:45:36
typescript
Slav4ka, 2021-12-01 00:45:36

How to fix component type error?

encountered an error

TS2345: Argument of type 'string | string[] | undefined' is not assignable to parameter of type 'T'. 'string | string[] | undefined' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | string[] | undefined'. Type 'undefined' is not assignable to type 'T'. 'undefined' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | string[] | undefined'.


Component code:

interface IFilters<U extends string | string[] | undefined> {
  setData(filterType: FiltersType, prevValue: U, value: U): void;
  buttonsGroup: JSX.Element;
  source: "partners" | "accounts";
  config: {
    type: FiltersType;
    isMulti: boolean;
    label: string;
    placeholder: string;
    labelClass: string;
  }[];
}

const Filters = <T extends string | string[] | undefined>({
  setData,
  config,
  buttonsGroup,
  source,
}: IFilters<T>): JSX.Element => {
  return (
    <div className={styles.partnersFilters}>
      {config.map((filter) => (
        <Filter<string | string[] | undefined>
          type={filter.type}
          source={source}
          isMulti={filter.isMulti}
          setData={(prevValue, value) => setData(filter.type, prevValue, value)}
          label={filter.label}
          placeholder={filter.placeholder}
          labelClass={filter.labelClass}
          key={filter.type}
        />
      ))}
      {buttonsGroup}
    </div>
  );
};


call:
<Filters<string[]> config={filtersConf} source="partners" setData={setData} buttonsGroup={<ButtonsGroup />} />

61a69b69c1543998477230.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2021-12-01
@Alexandroppolus

And what type is the setData prop for Filter? What value comes into it?

setData={(prevValue, value) => setData(filter.type, prevValue, value)}

Here you are trying to assign this value to T

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question