Update:
At this point, I'm no longer able to repro it on local, after I ran
rm -rf node_mdoules/ && npm i
I'll keep the question, as it might be useful for others experiencing this bug. At the point of this writing, the bug is still reproducible in jsfiddle.
If someone finds a way to repro it reliably, do provide a repo as I know a few passionate coders curious enough to search for its root cause.
Initial question
I have this JavaScript code, which works:
const iconsMap = new Map([
['Roofs', 'roofs'],
['Walls', 'walls'],
['Windows & Doors', 'doors'],
['Heating & Controls', 'heating'],
['Ventilation & Air Tightness', 'ventilation'],
['Renewables', 'renewables'],
['Others', 'others'],
['Default', 'home'],
]);
console.log(
Object.assign({}, ...Array.from(iconsMap).map(([label, fileName]) => ({[label]: fileName})))
);
My first Typescript attempt was very close to original JS:
const iconsMap: Map<string, string> = new Map([
['Roofs', 'roofs'],
['Walls', 'walls'],
['Windows & Doors', 'doors'],
['Heating & Controls', 'heating'],
['Ventilation & Air Tightness', 'ventilation'],
['Renewables', 'renewables'],
['Others', 'others'],
['Default', 'home'],
]);
interface Dictionary<T> {
[key: string]: T;
}
console.log(
Object.assign(
{} as Dictionary<string>,
...Array.from(iconsMap).map(
([label, fileName]) => ({ [label]: fileName })
)
)
);
But it breaks with:
VM325:13 Uncaught SyntaxError: Unexpected token '(' at new Function () at exec (VM319 typescript.js:41)
For some reason, the implied return of arrow function () => ({}) fails here. I need to do () => { return {}; }. So this works:
console.log(
Object.assign(
{} as Dictionary<string>,
...Array.from(iconsMap).map(([label, fileName]) => {
return { [label]: fileName };
})
)
);
Could anyone explain why?
Note: I'm not interested in alternative ways to rewrite the above code (it's actually taken out of context to highlight the issue). I already re-rewrote the whole thing, I'm using a .reduce() and I'm happy enough with it.
For the curious types... cripter, actual implementation:
export const iconRegistry: Dictionary<string> = Array.from(iconsMap)
.reduce((o, [label, fileName]) => ({
...o,
[label]: require(`./assets/icons/measure/${fileName}.svg`)
}), {} as Dictionary<string>);
But I'd like to understand why the implied return fails above.
Thanks for taking the time.
