Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

typescript - Array<Union of type>to Array<Union of generic type>

I'm trying to define the relationship between two array type, related by a Wrapper generic type:

interface WrapperType<T> {
  innerValue: T;
}

type InnerArrayType = (TypeA | TypeB | TypeC)[];
type WrappedArrayType = (WrapperType<TypeA>|WrapperType<TypeB>|WrapperType<TypeC>)[];

However, the information between the two union types. When one of them changes, the other one must be manually changed as well. I wonder if there is a reusable utility type that can take the InnerArrayType and the WrapperType and output WrappedArrayType.

type MagicUtility<T> = ???????

type WrappedArrayType = MagicUtility<InnerArrayType>;
// equivalent to
type WrappedArrayType = (WrapperType<TypeA>|WrapperType<TypeB>|WrapperType<TypeC>)[];

In my use case, the list of TypeA, TypeB, TypeC... is predefined, static, and finite. Note that they are NOT string types.

UPDATE

I was able to get Array<Generic of union> with the method below. So there is a way to convert Array<Generic of union> to Array<Union of generic>, the problem would be solved. However, it seems to be an impossible problem: https://github.com/Microsoft/TypeScript/issues/27272.

interface MyWrapper<T> {
    inner: T;
}

type ArrayOfUnionToArrayOfUnionOfGeneric<T> = 
  T extends (infer U)[]
  ? MyWrapper<U>[]
  : never;

type Inner = (1 | 2 | 3)[];
type Outer = ArrayOfUnionToArrayOfUnionOfGeneric<Inner> // MyWrapper<1 | 2 | 3>[]

// MyWrapper<1 | 2 | 3>[] --(HOW???)--> (MyWrapper<1>|MyWrapper<2>|MyWrapper<3>)[]
question from:https://stackoverflow.com/questions/65650306/arrayunion-of-typeto-arrayunion-of-generic-type

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is possible using a distributive conditional type: the U extends infer V part is what distributes over the union.

type MagicUtility<T extends any[]> =
  (T extends (infer U)[] ? U extends infer V ? WrapperType<V> : never : never)[]

Playground Link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...