我有以下函数,它遍历对象的所有属性并将它们从 ISO 字符串转换为日期:
function findAndConvertDates<T>(objectWithStringDates: T): T {
for (let key in Object.keys(objectWithStringDates)) {
if (ISO_REGEX.test(objectWithStringDates[key])) {
objectWithStringDates[key] = new Date(objectWithStringDates[key]);
} else if (typeof objectWithStringDates[key] === 'object') {
objectWithStringDates[key] = findAndConvertDates(
objectWithStringDates[key]
);
}
}
return objectWithStringDates;
}
TypeScript 不断告诉我Element implicitly has an 'any' type because type '{}' has no index signature
——指的是objectWithStringDates[key]
.
考虑到该对象是作为通用对象传入的,如果没有明确的索引签名,我将如何着手访问这些属性?
(否则如何提供索引签名或抑制此错误?)
谢谢!