Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

869 - DistributeUnions #22276

Open
goddnsgit opened this issue Jan 17, 2023 · 0 comments
Open

869 - DistributeUnions #22276

goddnsgit opened this issue Jan 17, 2023 · 0 comments
Labels
869 answer Share answers/solutions to a question en in English

Comments

@goddnsgit
Copy link

// your answers
// 把前面几位的代码翻看了几十遍后模仿着写了一边
// Merze { } & { } => {...} 
// UnionToIntersection { } | { } => { } & { } 
// DistObj { ... , ... } => { _temp:{} } | { _temp:{} } => { _temp:{} } & { _temp:{} } => {} & {}
// ----------------------------------------------------------------------------
type Merze<T> = T extends Function ? T : { [K in keyof T]: T[K] };
// ----------------------------------------------------------------------------
type UnionToIntersection<U>
    = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
// ----------------------------------------------------------------------------
type ValueExt<K, V> = V extends V ? { [_ in K & string]: V } : never
// ----------------------------------------------------------------------------
type DistObj<T, K extends keyof T = keyof T> = Merze<
    UnionToIntersection<
        K extends K ? { _temp: ValueExt<K, DistributeUnions<T[K]>> } : never
    > extends { _temp: infer O } ? O : {}>
// ----------------------------------------------------------------------------
type ArrExt<A extends any[], U> = A extends A ? U extends U ? [U, ...A] : never : never
// ----------------------------------------------------------------------------
type DistArr<T> = T extends [infer U, ... infer L] ? ArrExt<DistArr<L>, DistributeUnions<U>> : []
// ----------------------------------------------------------------------------
type DistributeUnions<T>
    = T extends readonly any[]
    ? DistArr<T>
    : T extends Function
    ? T // 这个似乎还得完善一下。。。
    : T extends { [_: string]: any }
    ? DistObj<T>
    : T
// ----------------------------------------------------------------------------
@goddnsgit goddnsgit added answer Share answers/solutions to a question en in English labels Jan 17, 2023
@github-actions github-actions bot added the 869 label Jan 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
869 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant