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

Port std_merge.js to typescript #170

Open
dlespiau opened this issue Apr 21, 2019 · 2 comments
Open

Port std_merge.js to typescript #170

dlespiau opened this issue Apr 21, 2019 · 2 comments
Labels
kind/enhancement New feature or request topic/std Relates to the standard library

Comments

@dlespiau
Copy link
Member

We probably want to solve #128 before looking at what the typescript version would look like.

@dlespiau dlespiau added kind/enhancement New feature or request topic/std Relates to the standard library labels Apr 21, 2019
@spion-h4
Copy link

spion-h4 commented Aug 30, 2019

Here is some help:

type ChooseItem<A, B, K> =
K extends keyof B ? 
    (K extends keyof A ? 
        MergeTypes<A[K], B[K]> : 
        B[K]) : 
    K extends keyof A ? A[K] : never;


type MergeIndividually<A, B> = {
    [K in keyof A]: ChooseItem<A, B, K>
} & {
    [K in keyof B]: ChooseItem<A, B, K>
}

type MergeTypes<A, B = A> = A extends object ? 
  (B extends object ? MergeIndividually<A, B> : B) : 
  B;


// Examples:

type Z = MergeTypes<number, string>;
type X = MergeTypes<{
    a: {a: number, b: number, z: {x: number, y: number}}
}, {
    a: {a: string, c: number, z: {x: number, y: string, t: string}}
}>

declare let v:X;

let m = v.a.z.y

Playground: http://bit.ly/2PmXu9X

@spion-h4
Copy link

But if you are okay with assuming that primitives are always of the same type, a simple type MergeTypes<A, B> = A & B will be sufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request topic/std Relates to the standard library
Projects
None yet
Development

No branches or pull requests

2 participants