Home > Blockchain >  extract property values from array
extract property values from array

Time:07-06

Is there some way to extract property values from array?

const d = [{id: 'Cat'}, {id: 'Dog'}]

type ids = ??? //insert code here, type should be 'Cat' | 'Dog'

(It would also work if it generates a const enum)

CodePudding user response:

When you declare d with as const, you can extract the information like this:

const d = [{id: 'Cat'}, {id: 'Dog'}] as const

type IDs = typeof d[number]["id"]

Playground

  • Related