Typescript typed Map I have the following code:
    export type AvatarSize = &quot;xs&quot; | &quot;sm&quot; | &quot;md&quot;;
    
    const sizes = new Map&lt;AvatarSize, 32 | 44 | 104&gt;([
      [&quot;xs&quot;, 32],
      [&quot;sm&quot;, 44],
      [&quot;md&quot;, 104]
    ]);

To me, it feels dirty. I wonder if there is any other way to write it without hard-coding the sizes?
Thanks!
You can declare the array passed into the constructor as const so it doesn't get widened to Array&lt;[string, number]&gt;, and use generics to look up the key on the constructed map to get the AvatarSize type:
const sizes = new Map([
    [&quot;xs&quot;, 32],
    [&quot;sm&quot;, 44],
    [&quot;md&quot;, 104]
] as const);
export type AvatarSize = Parameters&lt;typeof sizes.get&gt;[0];

Another way of doing it would be to declare the array outside:
const sizesArr = [
    [&quot;xs&quot;, 32],
    [&quot;sm&quot;, 44],
    [&quot;md&quot;, 104]
] as const;
const sizes = new Map(sizesArr);
export type AvatarSize = (typeof sizesArr)[number][0];