最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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];

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];
Share Improve this question asked Jan 20, 2021 at 17:20 Bruno PintosBruno Pintos 4811 gold badge5 silver badges15 bronze badges 1
  • why are you using as ... it should know what that is automatically. – Get Off My Lawn Commented Jan 20, 2021 at 17:26
Add a comment  | 

1 Answer 1

Reset to default 16

You can do

let name: keyof typeof myObject

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论