Bruno Pintos
Bruno Pintos

Reputation: 481

A 'const' assertions can only be applied to references to enum members, or string, number, boolean, array, or object

Object.keys() as const not working. How can I achieve this? (Suppose I don't know the content of the object, I don't know what keys does my object have)

const values = Object.keys(myObject) as const;

I need the as const to get string literal types

let name: typeof values[number];

Upvotes: 18

Views: 14079

Answers (1)

Rubydesic
Rubydesic

Reputation: 3476

You can do

let name: keyof typeof myObject

See this question for why strongly typing Object.keys might be a bad idea.

Upvotes: 16

Related Questions