Lately, I've been relying heavily on the code snippet from an answer that I requested:
function sameValuesAsKeys<K extends string>(...values: K[]): {readonly [P in K]: P} {
const ret = {} as {[P in K]: P}
values.forEach(k => ret[k] = k);
return ret;
}
However, recently the Eslint tool integrated with Angular has started flagging this function.
The error message suggests using const or class constructors instead of named functions
I attempted to revise the code like this:
const sameValuesAsKeys:<K extends string>=(...values: Array<K>): {readonly [P in K]: P} => {
const ret = {} as {[P in K]: P};
values.forEach(k => ret[k] = k);
return ret;
}
Unfortunately, this resulted in an error related to not being able to find K
. Another approach I tried was:
const TEST={
sameValuesAsKeys(...values: Array<K>):<K extends string> => ({readonly [P in K]: P}) => {
But this also failed. Any advice on how to properly format it to resolve the issue highlighted by ESlint?