英文:
Getting values of all id as type from an object
问题
I have an object like
var myObj = [{ id: 'a', value: 1}, { id: 'b', value: 2 }, { id: 'c', value: 3}]
and I have a function to which I want to pass just possible values of ids
function abc(id: 'a' | 'b' | 'c') { /* */ }
I tried like this
var myObj = [{ id: 'a', value: 1}, { id: 'b', value: 2 }, { id: 'c', value: 3}]
type OJ = typeof myObj;
type id = keyof OJ[number]['id'];
but it's showing id
as:
type id = number | typeof Symbol.iterator | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | ... 30 more ... | "padEnd"
英文:
I have an object like
var myObj = [{ id: 'a', value: 1}, { id: 'b', value: 2 }, { id: 'c', value: 3}]
and I have a function to which I want to pass just possible value of ids
function abc(id: 'a' | 'b' | 'c') { /* */ }
I tried like this
var myObj = [{ id: 'a', value: 1}, { id: 'b', value: 2 }, { id: 'c', value: 3}]
type OJ = typeof myObj;
type id = keyof OJ[number]['id'];
but its showing id as
type id = number | typeof Symbol.iterator | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | ... 30 more ... | "padEnd"
答案1
得分: 1
以下是翻译好的内容:
如果你从 type id = keyof OJ[number]['id'];
中移除 keyof
,你的错误将会被修复。这将使 id
与 string
匹配。如果你想要更具体的类型,例如,你希望 id
为 "a" | "b" | "c"
,那么你可以在对象后面添加 as const
。
function abc(id: 'a' | 'b' | 'c') { /* */ }
var myObj = [{ id: 'a', value: 1}, { id: 'b', value: 2 }, { id: 'c', value: 3}] as const
type OJ = typeof myObj;
type id = OJ[number]['id'];
// ^?
英文:
Your error will be fixed if you remove keyof
from type id = keyof OJ[number]['id'];
. This will make id
match string
. If you want more specific type, ie, you want id
to be "a" | "b" | "c"
, then you can give as const
after the object.
function abc(id: 'a' | 'b' | 'c') { /* */ }
var myObj = [{ id: 'a', value: 1}, { id: 'b', value: 2 }, { id: 'c', value: 3}] as const
type OJ = typeof myObj;
type id = OJ[number]['id'];
// ^?
答案2
得分: 0
不需要在你的 type id
中使用 keyof
:
const myObj = [{ id: 'a', value: 1}, { id: 'b', value: 2 }, { id: 'c', value: 3}];
type OJ = typeof myObj;
type id = OJ[number]['id'];
然而,使用这种方法,id
的类型将是 string
,因为 TypeScript 认为 myObj
是开放的,你可以随后向 myObj
添加其他元素。
为了防止这种情况,可以使用 const
断言:
const myObj = [{ id: 'a', value: 1}, { id: 'b', value: 2 }, { id: 'c', value: 3}] as const;
type OJ = typeof myObj;
type id = OJ[number]['id'];
英文:
You don't need the keyof
in your type id
:
const myObj = [{ id: 'a', value: 1}, { id: 'b', value: 2 }, { id: 'c', value: 3}];
type OJ = typeof myObj;
type id = OJ[number]['id'];
With this approach, however, the type of id
will be string
, because TypeScript considers myObj
to be open-ended and you could add additional elements to the myObj
later on.
To prevent this, use a const
assertion:
const myObj = [{ id: 'a', value: 1}, { id: 'b', value: 2 }, { id: 'c', value: 3}] as const;
type OJ = typeof myObj;
type id = OJ[number]['id'];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论