score:0

const a = Object.freeze({
  REMOVE_ITEM: 'REMOVE_ITEM',
  ADD_ITEM: 'ADD_ITEM'
} as const);

// assign typeof 
const b: typeof a = {} as any; // you can ignore `{} as any` - only here for example

b; // type is { REMOVE_ITEM: 'REMOVE_ITEM', ADD_ITEM: 'ADD_ITEM' }
b.REMOVE_ITEM; // = 'REMOVE_ITEM'
b.ADD_ITEM; // = 'ADD_ITEM'

// destructure
const { REMOVE_ITEM, ADD_ITEM } = a;

REMOVE_ITEM; // = 'REMOVE_ITEM'
ADD_ITEM; // = 'ADD_ITEM'

score:0

enum MyEnum {
  FOO_BAR = 'foo_bar',
  LORD_OF = 'the_rings'
}
type MyOhterType = {
  fooBar: MyEnum
}

const myOtherVar: MyOtherType = {
  fooBar: MyEnum.FOO_BAR
}

score:1

const b = Object.keys(a);
// to JavaScript: will be ['REMOVE_ITEM', 'ADD_ITEM']
// to TypeScript: string[]

// or, to get stronger typings for `b` rather than just `string[]`:

const b = Object.keys(a) as (keyof typeof a)[];
// to JavaScript: will be ['REMOVE_ITEM', 'ADD_ITEM']
// to TypeScript: ('REMOVE_ITEM' | 'ADD_ITEM')[]


Related Query

More Query from same tag