[英]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
}
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.
属性的结果类型。
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.