简体   繁体   English

你如何定义一个可以递归地成为 object 的任何子树的类型?

[英]How do you define a type which could be any subtree of an object recursively?

Imagine that I have the following data:想象一下,我有以下数据:

const data = {
  animilia: {
    chordata: {
      mammalia: {
        carnivora: {
          canidae: {
            canis: 'lupus',
            vulpes: 'vulpe'
          }
        }
      }
    }
  },
} as const;

Now I have a component which will render this out as a tree, recursively, like so:现在我有一个组件,它将递归地将其呈现为树,如下所示:

function RecursiveList({ list, path = [] }) {
  return (
    <ul>
      {Object.entries(list).map(([key, value]) => {
        const reactKey = [...path, key].join('.');
        if (isString(value)) {
          return <li key={reactKey}>{value}</li>
        }
        return (
          <Fragment key={reactKey}>
            <li>{key}</li>
            <RecursiveList list={value} path={[...path, key]} />
          </Fragment>
        )
      })}
    </ul>
  );
}

The problem I'm having is defining the type for list .我遇到的问题是定义list的类型。 In theory, it should be the provided type at any depth.理论上,它应该是任何深度的提供类型。 I tried the following:我尝试了以下方法:

type Tree<T extends string | Record<string, unknown>> = {
  [K in keyof T]: T extends string ? string : Tree<T>;
}

This doesn't seem to accurately reflect the type.这似乎不能准确地反映类型。

Is there a way to define data like this that would be a recursive tree?有没有办法定义这样的数据,这将是一个递归树?

For the purposes of the React component we don't need any conditionals or generics or anything fancy.对于 React 组件,我们不需要任何条件或 generics 或任何花哨的东西。 @jonrsharpe has the right idea in his comment. @jonrsharpe 在他的评论中有正确的想法。 A Tree is an object where each key is a string and each value is either a string or another Tree . Tree是 object ,其中每个键都是string ,每个值都是string或另一个Tree

interface Tree {
  [key: string]: Tree | string
}
function RecursiveList({ list, path = [] }: {list: Tree, path?: string[]}) {

This works perfectly in the component.这在组件中完美运行。 The value inside your Object.entries has type string | Tree Object.entries中的value具有类型string | Tree string | Tree . string | Tree If it's a string we print it, and if it's a Tree we render another RecursiveList from this tree.如果它是一个string ,我们打印它,如果它是一个Tree ,我们从这棵树渲染另一个RecursiveList

Typescript Playground Link Typescript 游乐场链接

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

相关问题 如何在JavaScript中定义可以包含任何字符的字符串? - How do you define a string in JavaScript that could contain any character? 如果在匿名函数中定义对象 - 如何使用它? - If you define an object in an anonymous function — how do you use it? Typescript:如何用布尔值或回调函数定义联合类型? - Typescript: how do you define a union type with boolean or a callback function? 你如何定义在 Typescript 中采用两个 object 类型的函数? - How do you define functions that take two object types in Typescript? 如何在参数中定义 object 数据类型? - How do I define object data type in parameter? 如何在嵌套 object / 数组数据结构的任何级别定义确切的数据类型? - How could one define the exact data types at any level of a nested object / array data-structure? 如何定义CKEditor用于搜索配置/语言文件的路径? - How do you define the path which CKEditor uses to search for config / language files? 如何在化简器(ReactJS + Redux)中的数组的数组内定义对象的状态? - How do you define a state of an object inside an array of an array in a reducer (ReactJS + Redux)? 你如何在 JavaScript 中定义一个 OOP 类? - How do you define an OOP class in JavaScript? 您如何在PHP中定义索引? - How do you define an index in PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM