I declare a map so I can convert the numeric enum value into the value our API expects.
export const StatusMap: ReadonlyMap<Status, string> = new Map([
[Status.NEW, 'new'],
[Status.PENDING, 'pending'],
]);
But when I do statusMap.get(Status.NEW) it always tells me that the return value is possibly undefined. Is there a way to force a map (or similar) to contain all enum values?
And yes I know you can technically do
export enum Status {
NEW = 'new',
PENDING = 'pending',
}
but let's be honest, this kind of ruins the point of enums (IMO).
Mapit will be as simple asRecord<Status, string>. But what's the point of mapping strings to numbers and vice versa if the api expects the strings? String enum doesn't ruin the point of enumstype Status = 'new' | 'pending';type StatusMap = { get<T extends Status>(status: T): string }and use it forstatusMap. Pay attention, it won't verify that all enum values are actually in map typescriptlang.org/play/index.html#code/…