This error occurs when you try to access an object property using a string index, but the object type does not have an index signature that accepts a string parameter. TypeScript requires that you either define an index signature on the object type or use a more specific type for the index parameter.

To fix this error, you can add an index signature to the object type as follows:

type MyObject = {
  SetTop: boolean;
  AutomaticStartUp: boolean;
  RemoveSource: boolean;
  RemoveRestore: boolean;
  [key: string]: boolean; // index signature that accepts any string key
}

Alternatively, you can use a more specific type for the index parameter if you know the exact keys that will be used:

const myObject: {
  [key in 'SetTop' | 'AutomaticStartUp' | 'RemoveSource' | 'RemoveRestore']: boolean;
} = {
  SetTop: true,
  AutomaticStartUp: false,
  RemoveSource: true,
  RemoveRestore: false,
};

const value = myObject['SetTop']; // no error

By specifying the keys in the index parameter, TypeScript can infer the correct type for the property value and prevent this error from occurring

TS7053 Element implicitly has an any type because expression of type string cant be used to index type SetTop boolean; AutomaticStartUp boolean; RemoveSource boolean; RemoveRestore boolean; No ind

原文地址: https://www.cveoy.top/t/topic/eiI9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录