The following code

const statusMap = {
    [STATUS_ID.TO_CONFIRM]: Lang.padding,`
    [STATUS_ID.TO_LOGIN]: Lang.checkIn,`
    [STATUS_ID.TO_LOGOFF]: Lang.checkOut,`
    [STATUS_ID.LOGOFF]: Lang.rOut,`
    [STATUS_ID.REJECTED]: Lang.rrefuse,`
    [STATUS_ID.EXPIRED]: Lang.overdue,`
    [STATUS_ID.CANCEL]: Lang.rrevoke,`
    [STATUS_ID.SYSLOGOFF]: Lang.systemSignOff,`
};
return statusMap[status];
Copy the code

If you write this, you will report a check error for TS

Because statusMap only contains keys 11-18, ts will report an error when passing an uncertain value of status

So the problem can be solved by adding as any to the end of the object.

Solution: Use Map

The following code

export const STATUS_ID: [number, string][] = [ [11, 'padding'], [12, 'checkIn'], [13, 'checkOut'], [14, 'rOut'], [15, 'rrefuse'], [16, 'overdue'], [17, 'rrevoke], [18,' systemSignOff],]; export const renderStatusText = (status: number, Lang: any) => { const newMap = new Map<number, string>(STATUS_ID); const res = newMap.get(status) as string; return Lang[res];Copy the code

The STATUS_ID is also written in the other file, which is equivalent to renderStatusText with only three lines of code, much more concise. New Map() accepts an array (just like new Set()) and returns a variable of type Map. The actual array is written in the form shown in the code. Const res = newmap.get (status) as string; const res = newmap.get (status) as string; I don’t know if there’s a better way to make this get return a string (currently ts thinks it might return undefined). I think this is the best solution, but writing this way would make the set of constants an array instead of an enum, which is a small flaw