How to type a hashtable whose keys can only be members of an enum?

12 Views Asked by At

In Typescript I can add a type for a hashtable foo whose keys are members of an enum Flag with foo: { [key in Flag]: any}. However, this requires that foo contains a key-value pair for every member of the Flag enum. What if I just want it to be partial - to say that the keys in foo can only be members of Flag, but I'm not expecting every member of Flag?

1

There are 1 best solutions below

0
On

Add a question mark to mark the keys as optional:

foo: { [key in Flag]?: any}