TypeScript: is it possible to create a mapped union type?

Is it possible to combine the functions "associated types" and "union types" to create an expression that accepts the following interface as input:

interface AwaActionTypes {
  CLICKLEFT: 'CL';
  CLICKRIGHT: 'CR';
  SCROLL: 'S';
  ZOOM: 'Z';
  RESIZE: 'R';
  KEYBOARDENTER: 'KE';
  KEYBOARDSPACE: 'KS';
  OTHER: 'O';
}

And creates a type equivalent to the following union type alias:

type AwaActionType: 'CL' | 'CR' | 'S' | 'Z' | 'R' | 'KE' | 'KS' | 'O';

I tried using combinations keyof, |etc. Didn't land on something that worked. I did not see anything in the handbook .

+4
source share
1 answer

This is a combination of keyof and search type

type AwaActionType = AwaActionTypes[keyof AwaActionTypes];
+7
source

Source: https://habr.com/ru/post/1676197/


All Articles