从对象中获取所有ID值的类型。

huangapple go评论97阅读模式
英文:

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,你的错误将会被修复。这将使 idstring 匹配。如果你想要更具体的类型,例如,你希望 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'];
//    ^?

playground

英文:

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'];
//    ^?

playground

答案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'];

huangapple
  • 本文由 发表于 2023年3月7日 16:28:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定