In the process of creating a small library, I am focused on enhancing type safety for the following object:
export const ErrorCodeMap: Record<string, [string, number]> = {
NOT_FOUND: ['1001', 222],
ALREADY_EXISTS: ['1002', 111],
}
My goal is to obtain a type-safe list of the keys within the object ErrorCodeMap
.
Upon attempting the following:
export type ErrorCode = keyof typeof ErrorCodeMap;
I noticed that the type of ErrorCode
is simply string
, whereas I am aiming for it to be 'NOT_FOUND' | 'ALREADY_EXISTS'
.
My attempts with the as const
syntax have not yielded the desired outcome.
Is there a solution available that does not involve altering the Record<string, [string, number]>
definition and thus preserving type safety in the object's structure?