简体   繁体   English

在 Typescript 中,如何允许类型具有指向该类型递归值的泛型的所有键?

[英]In Typescript how do I allow a type to have all keys of a generic that point to a recursive value of that type?

Right now I have something like this where "any" can act as the generic "V"现在我有这样的东西,其中“any”可以充当通用的“V”

interface Validation<V> {
  $isValid: boolean
  $isValidating: boolean
  $value: V
  [prop: string]: boolean | V | Validation<V>
}

What I'd like to do is replace the string indexing type with any string key K from V will return a sub Validation interface.我想要做的是用来自 V 的任何字符串键 K 替换字符串索引类型将返回一个子验证接口。

interface Validation<V> {
  $isValid: boolean
  $isValidating: boolean
  $value: V
  [K extends Extract<keyof V, string>]: Validation<V[K]>
}

This clearly doesn't work and curious to know if something similar is achievable.这显然行不通,并且很想知道是否可以实现类似的目标。

Yes.是的。 Here is how to do it这是如何做到的

type Validation<V> = {
    [K in Extract<keyof V, string>]: Validation<V[K]>
} & {
    $isValid: boolean
    $isValidating: boolean
    $value: V
}
  1. You should use mapped type , so you should write K in ... .您应该使用映射类型,因此您应该K in ...写入K in ...

    The syntax resembles the syntax for index signatures with a for .. in inside.语法类似于索引签名的语法, for .. in里面有一个for .. in There are three parts:分为三个部分:

    • The type variable K, which gets bound to each property in turn.类型变量 K,依次绑定到每个属性。

    • The string literal union Keys, which contains the names of properties to iterate over.字符串文字联合键,其中包含要迭代的属性名称。

    • The resulting type of the property.属性的结果类型。
  2. To dd additional properties use intersection , as it is not possible to add additional properties to mapped type directly.要添加附加属性,请使用intersection ,因为无法直接向映射类型添加附加属性。

暂无
暂无

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

相关问题 打字稿:如何制作接受键匹配泛型但所有值都是值参数的映射函数的对象的类型 - Typescript: How to make type that accepts object which keys match generic but all values are map functions of value argument 如何在 TypeScript 中为递归(如 s 表达式)定义泛型类型别名? - How do I define a generic type alias for something recursive (like an s-expression) in TypeScript? 如何键入接收通用联合类型数组的递归 function? (打字稿) - How do I type a recursive function receiving an array of generic union types? (typescript) 如何调试递归 TypeScript 泛型类型 - How to debug recursive TypeScript generic type 在使用通用初始值设定项 function 时,如何让 typescript 正确推断类型? - How do I have typescript have the type properly infered when using a generic initializer function? 如何获取泛型类型(TypeScript)的键列表? - How to get a list of keys of generic type (TypeScript)? 如何为具有通用键的字典定义 typescript 类型? - How to define a typescript type for a dictionary with generic keys? Typescript 带有条件的递归泛型类型? - Typescript recursive generic type with conditions? 如何制作 typescript 类型,其中 object 中的所有键都具有相同的前缀? - How can I make a typescript type where all keys in object have same prefix? 如何创建应具有数组类型的所有键的 object 类型 - How do I create an object type that should have all the keys of an array type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM