Here is the TypeScript playground example for this question, which only has the bare minimum required for this question.
Here is the real world example of where this comes up: I have an enum
export enum OrderType {
Market = 'MARKET',
Limit = 'LIMIT',
}
And then I have an input field
export const TypeInputInner = ({ onChange, value }: Props) => {
return (
<Select style={{ width: 150, margin: 5 }} value={value} onChange={onChange}>
{Object.keys(OrderType).map(type => (
<Option key={type} value={OrderType[type]}>
{type}
</Option>
))}
</Select>
);
};
There is a TS error on that OrderType[type]
part:
Error:(16, 45) TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.
Why is this? Isn't the enum just an object?, ie
{
Market: "MARKET",
Limit: "LIMIT"
}
Where do numbers come in here?
Btw, I know that if I use type as any
the TS errors go away.