in non .tsx pages we used to enum like this:
const enumUrl= {
xxx: [BRAND],
yyy: [BRAND],
};
In .tsx pages I would like to use enum
. So I created:
enum EnumUrl {
xxx = "https://example.com/",
yyy = "https://example.net"
}
And in JSX:
Visit <a href={EnumUrl[BRAND}} target="_blank" rel="noopener noreferrer">
{EnumUrl[BRAND]}
</a>
However it says:
Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'typeof EnumUrl'.ts(7053)
Then I read this possible solution: https://stackoverflow.com/a/41970976/1580094 and did the following:
enum EnumUrl {
xxx = "https://example.com/",
yyy = "https://example.net"
}
var url : EnumUrl = EnumUrl[BRAND as keyof typeof EnumUrl];
...
Visit <a href={url[BRAND}} target="_blank" rel="noopener noreferrer">
{url[BRAND]}
</a>
Console logs: console.log(url); // https://example.com/ console.log(url[BRAND]); // undefined console.log(BRAND); // xxx
But doing this way, the <a
element completely disappears from DOM. No errors.
What am I doing wrong and how can I fix it?