简体   繁体   English

如何从 Typescript 中类型化的 object 的属性值生成类型?

[英]How can I generate a type from the properties values of a typed object in Typescript?

Say I had an interface that described a library that items that look like this:假设我有一个interface ,该界面描述了一个库,其中的项目如下所示:

interface MyItem {
  category: string,
  title: string
}

Now I have a config file full of those MyItems:现在我有一个包含这些 MyItems 的配置文件:

const myLibrary: MyItem[] = [
  {
    category: "dogs",
    title: "Fuzzy quadrupeds" 
  },
  { 
    category: "snakes",
    title: "Slithery reptiles"
  },
  ...
]

Now, I'd like to create a type that consists of all the category in MyItem[]现在,我想创建一个包含MyItem[]中所有category的类型

If I do this: type Category = typeof MyItem[number]["category"] I get string .如果我这样做: type Category = typeof MyItem[number]["category"]我得到string

If I remove the typing from myLibrary (ie const myLibrary = [ {...} ] ) and get what I want:如果我从myLibrary中删除输入(即const myLibrary = [ {...} ] )并得到我想要的:

That means that type Category = typeof MyItem[number]["category"] gives me the union type I want of dogs | snakes这意味着type Category = typeof MyItem[number]["category"]给了我我想要的dogs | snakes dogs | snakes but of course I lose the typing when creating new items in my config file. dogs | snakes ,但当然我在我的配置文件中创建新项目时会丢失打字。

We want to restrict the items in myLibrary such that they must implement MyItem , but we want to do it in a way that preserves specific types of specific items and doesn't broaden the type to just MyItem .我们希望限制myLibrary中的项目,以便它们必须实现MyItem ,但我们希望以保留特定项目的特定类型并且不将类型扩大到仅MyItem的方式来做到这一点。

It is hard to do that just with assigning a type to the constant.仅通过为常量分配类型很难做到这一点。 A commonly-used pattern is to create the constant through an identity function.一种常用的模式是通过标识 function 创建常量。 With a function we can use extends syntax to ensure that T extends MyItem[] while keeping T specific.使用 function 我们可以使用extends语法来确保T extends MyItem[]同时保持T特定。

I had to use as const to get the literal category names, so I also had to allow readonly in the function arguments.我必须使用as const来获取文字类别名称,所以我还必须在 function arguments 中允许readonly

interface MyItem {
  category: string,
  title: string
}

const buildLibrary = <T extends readonly MyItem[]>(library: T): T => library;

const myLibrary = buildLibrary([
  {
    category: "dogs",
    title: "Fuzzy quadrupeds" 
  },
  { 
    category: "snakes",
    title: "Slithery reptiles"
  }
] as const);

type Categories = (typeof myLibrary)[number]['category'] // "dogs" | "snakes"

Typescript Playground Link Typescript 游乐场链接

If I understand you right, you want this: How to create enum like type in TypeScript?如果我理解正确,你想要这个: How to create enum like type in TypeScript? and then specify MyItem as然后将 MyItem 指定为

interface MyItem: {
    category: MyDogSnakeType,
    title: string
}

Do not complicate不要复杂化


type Categorías = 'Food' | 'categoy1' | 'cstegory2'

interface MyItem {
  category: Categories;
  title: string
}

const myLibrary: MyItem[] = [
  {
    category: "dogs",
    title: "Fuzzy quadrupeds" 
  },
  { 
    category: "snakes",
    title: "Slithery reptiles"
  },
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从TypeScript中的匿名对象轻松创建强类型对象? - How can I easily create a strongly typed object from an anonymous object in TypeScript? 如何在typescript定义中指定松散类型的对象文字? - How can I specify loosely typed object literal in typescript definition? 在 TypeScript 中生成一个 const 类型的 object - Generate a const typed object in TypeScript Typescript:从 Object 键和数组字符串值生成类型 - Typescript: Generate Type from Object keys and Array string values How to map values from nested Typescript object to properties of JSON object - How to map values from nested Typescript object to properties of JSON object TypeScript:动态设置类型化 object 属性 - TypeScript: Dynamically set typed object properties 遇到错误:如何从打字稿中正确使用已翻译并键入的打字稿npm模块? - Getting errors: How can I properly consume my transpiled and typed typescript npm module from typescript? 如何在 Typescript 中将类型的属性作为数组获取? - How can I get properties of a type as an array in Typescript? TypeScript:如何键入一个 object,其属性是稍后添加的? - TypeScript: How to type an object with properties that are added later? TypeScript:如何初始化 object 并根据其类型提供一些默认值 - TypeScript: how do I initialize an object and give some default values from its type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM