英文:
define a typescript type that can contain only one single property
问题
如何在 TypeScript 类型中表示这个 JSON:
type MetaItem = Record<string, string>;
type Meta = MetaItem[];
meta 是一个由 meta_item 对象组成的数组,其中每个 meta_item 只有一个属性。
meta_item 的键应该是一个 URI,值可以是任何内容。
这样表示是否可行?
英文:
I have this JSON :
"meta" : [
{"http://example.com/rel/1/" : "my meta 14"},
{"http://example.com/rel/2/" : "345"}
],
How can I express this in a typescript type ?
meta is an array of meta_item objects,
where each meta_item has only one property.
The key of meta_item should be an URI, the value can be anything.
Is this possible ?
Thanks !
答案1
得分: 0
如果我有选择,我可能会选择使用字典:
const meta: { [id: string] : any; } = {};
meta["http://example.com/rel/1/"] = "my meta 14"
meta["http://example.com/rel/2/"] = "345"
或者,如果你想要数组:
type meta_item = {
[url: string]: any;
};
meta: meta_item[];
英文:
If I had the choice, I'd probably go for a dictionary:
const meta: { [id: string] : any; } = {};
meta["http://example.com/rel/1/"] = "my meta 14"
meta["http://example.com/rel/2/"] = "345"
Or, if you want the array:
type meta_item = {
[url: string]: any;
};
meta: meta_item[];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论