I’m having issues accessing a property in a object using a string index, where the interface is defined using in keyof
.
Take this code:
interface IFilm {
name: string;
author: string;
}
type IExtra<T extends {}> = {
[index in keyof T]: number;
};
const film : IFilm = { name: "FilmName", author: "AuthorName"};
const extra : IExtra<IFilm> = { name: 1, author: 2};
Object.keys(film).forEach(X => {
extra[x] = 3; // Error here!!
});
Where I try to access extra[x]
I got this error:
Element implicitly has an ‘any’ type because expression of type ‘any’ can’t be used to index type ‘IExtra’.
I’m getting why: typescript cannot know that x
contains only string that are actually valid for the constraint in keyof T
.
Thus, how can I rewrite this, or make TypeScript aware that this is valid code? I know I can using (extra as any)[x]
, but I’d like to be "type-checked" 🙂
2
Answers
You issue is that
Object.keys<T>
returns astring
, not akey of T
.You need yo do a type assertion
extra[x as keyof IFilm] = 3;
If you want to iterate over typed keys, use a
Map
.The
Object.keys
doesn’t returnkeyof T
, because TS does not guarantes that there are no other keys:If you are sure there is no other keys, make an alternative which knows that: