简体   繁体   English

TypeScript - 遍历字典类型中的类型并创建具有转换值的类型

[英]TypeScript - iterate over types in a dictionary type and create a type with transformed values

Note: This is not about runtime, we are talking purely about types here.注意:这不是关于运行时,我们在这里纯粹讨论类型。

Suppose there are types: Alpha<T> , Beta<T> and an example type like type Data = {a: Alpha<number>; b: Beta<string>, c: Alpha<string> }假设有类型: Alpha<T>Beta<T>和一个示例类型,例如type Data = {a: Alpha<number>; b: Beta<string>, c: Alpha<string> } type Data = {a: Alpha<number>; b: Beta<string>, c: Alpha<string> } I need to transform it to something like: {a: number; b: string; c: string } type Data = {a: Alpha<number>; b: Beta<string>, c: Alpha<string> }我需要将其转换为: {a: number; b: string; c: string } {a: number; b: string; c: string }

I know how to transform a single value, I have this implemented already, but I have no idea how could I iterate over a dictionary type with known properties and transform them.我知道如何转换单个值,我已经实现了,但我不知道如何迭代具有已知属性的字典类型并转换它们。 Is it even possible?甚至可能吗?

class Alpha<T> {
    private a: T;
}

class Beta<T> {
    private b: T;
}

// Extracts T from Alpha<T> or Beta<T> - already implemented
type GetClassParameterForAlphaBeta<T extends Alpha<any> | Beta<any>> =
    T extends Alpha<infer R>
    ? R : T extends Beta<infer R>
    ? R : unknown;

type Store = { [index: string]: Alpha<any> | Beta<any> }

// pseudo-code to give you an idea of what I'm after
type GetStoreData<T extends Store> = T each U : GetClassParameterForAlphaBeta<U>;

type Data = {a: Alpha<number>; b: Beta<string>, c: Alpha<string> }

type x = GetStoreData<Data> // Expected: {a: number; b: string, c: string }

One thing to note is that the 2 classes are known, but Data in the real case is a generic.需要注意的一点是这 2 个类是已知的,但实际情况下的Data是通用的。 It has a finite amount of properties, but it's all dynamic.它具有有限数量的属性,但都是动态的。 Manually applying the transformations is out of question.手动应用转换是不可能的。

Ah, you're just looking for a mapped type :啊,您只是在寻找映射类型

type GetStoreData<T extends Store> = {
  [K in keyof T]: GetClassParameterForAlphaBeta<T[K]>
}

You can verify that it works as expected:您可以验证它是否按预期工作:

type X = GetStoreData<Data> 
/* type X = {
    a: number;
    b: string;
    c: string;
} */

Playground link to code Playground 代码链接

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

相关问题 TypeScript 迭代 object 并从值创建一个类型 - TypeScript Iterate over object and create a type from the values Typescript 迭代记录类型并返回更新的记录 - Typescript iterate over a Record type and return updated Record 如何创建一个通用的 TypeScript 接口,该接口匹配任何类型/接口/对象,但其中的值类型有限制? - How to create a generic TypeScript interface which matches any type/interface/object with restrictions on types for values inside it? “pick”函数的 TypeScript 泛型类型(结果对象值类型) - TypeScript generic type for "pick" function (result object values types) Typescript type 用于两种具有相同键和相似值的类型 - Typescript type for two types with same keys and similar values Typescript - 从数组项的通用类型创建组合类型 - Typescript - Create Combined Type From Array Items' Generic Types 如何创建多个类型的集合并将它们与 Typescript 中的类型一起使用? - How to create a collection of multiple types and use them with their type in Typescript? 使用多个值的道具遍历 object 会导致 Typescript 错误:类型不可分配给类型 never - Iterate through object with props of multiple values leads to Typescript error: Type is not assignable to type never Typescript类型对Promise类型实施 - Typescript type enforce on promise types 遍历 TypeScript 中对象的键和值 - Iterate over object's keys and values in TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM