#Set key of object as the elements of an Array

4 messages · Page 1 of 1 (latest)

cunning elm
#
const keys = ['channelId', 'memberId'] as const;
const validOptions: Record<..., string[]> = {
  channelId: [...],
  memberId: [...],
};

In the above code, the keys of validOptions are the elements of keys array. But how do I tell that to typescript? In the above example, I used the Record helper, but really, I am fine with any method as long as it works well

crimson coyote
#

You could write this as:

const validOptions: Record<typeof keys[number], string[]> =
#

ArrayType[number] is a way to get the contents of an array as a type.

cunning elm
#

ty!