Reputation: 1127
How should I defined TS type for [key, val]
pair returned from Object.entries
?
type objType = {
one: string | null;
two: string | null;
};
const obj: objType = {
one: 'one',
two: 'two',
};
Object.entries(obj).map(([key, val]) => {
console.log(key, val);
});
EDIT: this was just a simplified example. The problem was in my code
Everything works as expected. Thanks everyone!
Upvotes: 0
Views: 226
Reputation: 478
You can do it like that:
type objType = {
[key: string]: string | null;
};
type objEntriesType = objType[];
const obj: objType = {
one: 'one',
two: 'two',
};
const result: objEntriesType[] = Object.entries(obj).map(([key, val]) => {
console.log(key, val);
});
Upvotes: 1
Reputation: 122
Do you mean define the types of the key/value pair of the .map
parameters like this?
Object.entries(obj).map(([key, val]: [Type1, Type2]) => {
console.log(key, val);
});
Upvotes: 1