@tostershmoster

Как исправить Element implicitly has an 'any' type because expression of type 'string' can't be used to index type?

function updateObjectValues(
  defaultSettings: IViewSettings | IModelSettings,
  userSettings: IUserSettings,
): IViewSettings | IModelSettings {
  const c: IViewSettings | IModelSettings = {};
  let key: string;

  // eslint-disable-next-line no-restricted-syntax
  for (key in defaultSettings) {
    // eslint-disable-next-line no-prototype-builtins
    if (defaultSettings.hasOwnProperty(key)) {
      c[key] = key in userSettings
        ? userSettings[key as keyof IUserSettings]
        : defaultSettings[key as keyof IDefSettings];
    }
  }

  return c;
}

interface IModelSettings {
  min: number;
  max: number;
  from: number;
  to: number;
  step: number;
  stepInPrecents: number;
  currentPos: number;
  curPosInPoints: number | undefined;
  posWithStepInPercents: number;
}

interface IViewSettings {
  range: boolean;
  scale: boolean;
  tooltips: boolean;
  vertical: boolean;
  confpanel: boolean;
  bar: boolean;

  thumbMarginFrom: number;
  thumbMarginTo: number;
  rangeMarginFrom: number;
  rangeMarginTo: number;
}

interface IUserSettings {
  min?: number;
  max?: number;
  from?: number;
  to?: number;
  step?: number;
  range?: boolean;
  scale?: boolean;
  vertical?: boolean;
  tooltips?: boolean;
  confpanel?: boolean;
  bar?: boolean;
}

type IDefSettings = IViewSettings | IModelSettings;


ошибка на c[key]

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'IViewSettings | IModelSettings'.
No index signature with a parameter of type 'string' was found on type 'IViewSettings | IModelSettings'.ts(7053)
  • Вопрос задан
  • 1643 просмотра
Решения вопроса 1
@tostershmoster Автор вопроса
function updateObjectValues(
  defaultSettings: IDefSettings,
  userSettings: IUserSettings,
): IViewSettings | IModelSettings {
  const c: IViewSettings | IModelSettings = {};

  const keys: string[] = Object.getOwnPropertyNames(defaultSettings);

  keys.forEach((key) => {
    c[key] = key in userSettings
      ? userSettings[key as keyof IUserSettings]
      : defaultSettings[key as keyof IDefSettings];
  });

  return c;
}


interface IModelSettings {
  min: number;
  max: number;
  from: number;
  to: number;
  step: number;
  stepInPrecents: number;
  currentPos: number;
  curPosInPoints: number | undefined;
  posWithStepInPercents: number;

  [index: string]: string | number | undefined;
}

interface IViewSettings {
  range: boolean;
  scale: boolean;
  tooltips: boolean;
  vertical: boolean;
  confpanel: boolean;
  bar: boolean;

  thumbMarginFrom: number;
  thumbMarginTo: number;
  rangeMarginFrom: number;
  rangeMarginTo: number;

  [index: string]: string | boolean | number;
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы