简体   繁体   English

如何在 Typescript 中深度扁平化界面?

[英]How to deep flatten an interface in Typescript?

for example:例如:

interface a {
  name: string 
  nested: {
    lastName: string 
    nested: {
      age: number
    }
  }
}

I want that to become:我希望它变成:

interface b {
  name: string 
  lastName: string 
  age: number
}

interface values will be dynamic, so I probably need some kind of recursive solution to this接口值将是动态的,所以我可能需要某种递归解决方案

I only ended up with finite solutions我只得到了有限的解决方案

Here's a solution using mapped and conditional types: FlattenPairs constructs a union of pairs like ['name', string] | ['lastName', string] | ['age', number]这是一个使用映射类型和条件类型的解决方案: FlattenPairs构造一个像['name', string] | ['lastName', string] | ['age', number]这样的对的并集。 ['name', string] | ['lastName', string] | ['age', number] ['name', string] | ['lastName', string] | ['age', number] , then Flatten converts this into an object type. ['name', string] | ['lastName', string] | ['age', number] ,然后Flatten将其转换为对象类型。 I assumed that the nested properties are not necessarily all named nested (and do not necessarily have predictable property names), so the type Primitive is needed as the base case for the recursion.我假设嵌套属性不一定都是命名nested (并且不一定具有可预测的属性名称),因此需要类型Primitive作为递归的基本情况。

type Primitive = string | number | boolean
type FlattenPairs<T> = {[K in keyof T]: T[K] extends Primitive ? [K, T[K]] : FlattenPairs<T[K]>}[keyof T] & [PropertyKey, Primitive]
type Flatten<T> = {[P in FlattenPairs<T> as P[0]]: P[1]}

Example:例子:

interface TestInterface {
  name: string 
  nested: {
    lastName: string 
    nested: {
      age: number
    }
  }
}

// {age: number, lastName: string, name: string}
type Test = Flatten<TestInterface>

Playground Link 游乐场链接

The helper type FlattenPairs can be written in a slightly simpler way if the nested property name(s) are known:如果已知嵌套属性名称,则可以以稍微简单的方式编写辅助类型FlattenPairs

type FlattenPairs<T> = {[K in keyof T]: K extends 'nested' ? FlattenPairs<T[K]> : [K, T[K]]}[keyof T]

Playground Link 游乐场链接

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM