Within an interface definition, I am utilizing a src member for an icon that can be either a string (representing an image path) or a predefined icon name:
interface IIconProperties {
src?: string | Codicon;
}
export enum Codicon {
add = "add",
}
When it comes to rendering, I need to create different outputs based on the value. However, since both are essentially strings, distinguishing between them is challenging:
if (typeof src === "string") {
// This condition is always true.
} else {
// This block is never reached.
}
I attempted to determine the type of the enum (src instanceof enum
, typeof src === "enum"
), but this syntax is not valid. How can I differentiate between these two types without resorting to using a separate lookup map (especially if I were to use numbers instead of strings in the enum)?