Is there a way to enforce a Map object has all literals of "a union type of literals" as keys, in TypeScript?

215 Views Asked by At

I want to create a type which is a Map has all possible literals of a string literal union type.

For example:

type TNumberNames =
  | 'one'
  | 'two'
  | 'three'

const numberMap: MyMapType = new Map().set('one', 1).set('two', 2).set('three', 3);
// this Map object should have all literals in TNumberNames!

I already found a way in Object instead of Map:

const numberRecord: Record<TNumberNames, number> = {
  one: 1,
  two: 2,
  three: 3,
};

in above example, if three is removed in numberRecord, TS compiler shows an error.

How do I declare MyMapType to do like this for Map object?

0

There are 0 best solutions below