Reputation: 173
Type 'string' cannot be assigned to type 'online' | 'offline'. I should be missing something,But I don't know how to do.
enum StatusOptions {
ONLINE = 'ONLINE',
OFFLINE = 'OFFLINE',
}
Object.keys(StatusOptions).map((item: keyof typeof StatusOptions) => {
return {
text: StatusOptions[key],
value: item
}
})
Upvotes: 0
Views: 50
Reputation: 1926
Type 'string' cannot be assigned to type 'online' | 'offline'
results from a mistyped function parameter, which you pass into your map function.
Object.keys
is defined as:
/**
* Returns the names of the enumerable string properties and methods of an object.
* @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.
*/
keys(o: object): string[];
Therefore you have to correctly type your function. Where item, is a string
Object.keys(StatusOptions).map((item: string) => /* do something with it */);
Just in case you want then use the item as a key for StatusOptions like: StatusOptions[item]
. You have to cast it as the following:
Object.keys(StatusOptions).map((item: string) => {
return {
text: StatusOptions[item as keyof typeof StatusOptions],
value: item
}
})
Upvotes: 1