Why is TypeScript reporting an error when using the keyof
type in the Utils.ts file?
export function escapeHtml(text: string) {
const characters = {
'&': '&',
'"': '"',
"'": ''',
'<': '<',
'>': '>'
};
return text.replace(/[<>&"']/g, function(x) {
return characters[x];
});
}
TypeScript is reporting the error Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ '&': string; '"': string; "'": string; '<': string; '>': string; }'. No index signature with a parameter of type 'string' was found on type '{ '&': string; '"': string; "'": string; '<': string; '>': string; }'.ts(7053)
when using the keyof
type in the Utils.ts file. Why is this happening?