type Merge<F, S> = { [K in keyof F | keyof S]: K extends keyof S ? S[K] : K extends keyof F ? F[K] : never }
Solution by eunsukimme #35240
type Merge<F, S> = {
[Key in keyof F | keyof S] : Key extends keyof S ? S[Key] : Key extends keyof F ? F[Key] : never
}
Solution by devshinthant #34610
type Merge<F extends any, S extends any> =
{
[k in keyof (F & S)]: k extends keyof S ? S[k] : k extends keyof F ? F[k] : never
}
Solution by Jayce-liang #34282
type Merge<F extends object, S extends object> = {
[P in keyof F | keyof S]: P extends keyof S ? S[P] : P extends keyof F ? F[P] : never
}
Solution by ouzexi #34019
type Merge<T , V> = { [K in keyof T | keyof V] : K extends keyof V ? V[K] : K extends keyof T ? T[K]:never }
Solution by rookiewxy #33750
type Merge<F, S> = {
[K in keyof F | keyof S] : K extends keyof S ? S[K] : K extends keyof F ? F[K] :never;
}
Solution by ckdwns9121 #33694
// 你的答案
type Merge<F extends Record<any, any>, S extends Record<any, any>> = {
[K in keyof F | keyof S]: K extends keyof S ? S[K] : F[K]
}
Solution by HelloGGG #33416
type Merge<F, S> = {
[P in keyof (F & S)]: P extends keyof S ? S[P] : P extends keyof F ? F[P] : never
}
Solution by loevray #33265
// 你的答案
type Merge<T, T2> = {
[K in keyof T | keyof T2]: K extends keyof T2 ? T2[K] : K extends keyof T ? T[K] : never;
};
Solution by 2531800823 #33247
type Merge<F, S> = {
[key in keyof F | keyof S]: key extends keyof F & keyof S
? S[key] :
key extends keyof F
? F[key]
: key extends keyof S ? S[key] : never;
}
Solution by ZhipengYang0605 #32832
type Merge<
F extends Record<keyof F, unknown>,
S extends Record<keyof S, unknown>,
I = F & S
> = {
[K in keyof I]:
K extends keyof S
? S[K]
: K extends keyof F
? F[K]
: never
}
Solution by ZhulinskiiDanil #32702
type Merge<F extends object, S extends object> =
{ [K in (keyof F | keyof S)]: K extends keyof S ? S[K] : K extends keyof F ? F[K] : never }
Solution by dev-hobin #32415
type Flatten<T> = {
[P in keyof T]: T[P]
}
type Merge<F, S extends Flatten<S>> = {
[K in keyof Flatten<F & S>]: Flatten<F & S>[K] extends never ? S[K] : Flatten<F & S>[K]
};
Solution by keyurparalkar #32298
// your answers
type Merge<F, S> = {
[K in keyof F | keyof S]: K extends keyof S
? S[K]
: K extends keyof F
? F[K]
: never
}
Solution by pea-sys #32218
type Merge<F, S> = Omit<F, keyof S> & S
Solution by rkamely #32062
type Merge<F extends Record<string, unknown>, S extends Record<string, unknown>> = {
[K in keyof F | keyof S]: K extends keyof F & keyof S
? S[K]
: K extends keyof F
? F[K]
: K extends keyof S
? S[K]
: never;
};
Solution by gasmg #32015
type Merge<F, S> = {
[P in keyof (F & S)] : P extends keyof (F | S) ? S[P] : (F & S)[P]
}
Solution by jinyoung234 #31814
type Merge<F, S> = {[key in keyof F | keyof S]: key extends keyof S ? S[key] : key extends keyof F ? F[key] : never }
Solution by kai-phan #31648
second argument overwrites first one
type Merge<F extends {}, S extends {}> = {[P in keyof (F & S)]: P extends keyof S ? S[P] : P extends keyof F ? F[P] : never}
Solution by ivannovazzi #31556
type Merge<F, S> = {
[K in keyof F | keyof S]: K extends keyof S
? S[K]
: K extends keyof F
? F[K]
: never;
};
Solution by vipulpathak113 #31514
type Copy<T> = {
[P in keyof T]: T[P];
}
type Merge<L, R> = Copy<Omit<L, keyof R> & R>;
Solution by sdrjs #31436
type Merge<F, S> = {
[K in keyof F | keyof S]: K extends keyof S
? S[K]
: K extends keyof F
? F[K]
: never
}
Solution by bicochan #31396
type Merge<F, S> = {
[M in (keyof F) | (keyof S)]: M extends keyof S
? S[M]
: M extends keyof F
? F[M]
: never;
};
Solution by HeeYeonKim98 #31358
type Merge<F, S> = {
[K in keyof F | keyof S]: K extends keyof S ? S[K] : K extends keyof F ? F[K] : never;
}
Solution by ricky-fn #31299
type Merge<F extends Record<string, any>, S extends Record<string, any>> = {
[k in keyof F | keyof S]: k extends keyof S ? S[k] : k extends keyof F ? F[k] : never
}
Solution by eward957 #31250
// 解答をここに記入
type Flattern<T> = { [P in keyof T]: T[P] }
type Merge<F, S> = Flattern<Omit<F, keyof (F | S)> & S>
keyof (F | S)
とすることで、共通の key を抽出できるので、F からこれを Omit します。
これと、S を結合すれば所望の型が得られます。
(※ Flatten は 型一致のために必要なだけなので、この問題の本質ではありません)
Solution by Kakeru-Miyazaki #30869
type Merge<F, S> = Omit<Omit<F, keyof S> & S, never>
Solution by dreamluo-plus #30552
type Merge<F, S> = { [P in keyof F | keyof S]: P extends keyof S ? S[P] : P extends keyof F ? F[P] : never }
Solution by MrRabbit1993 #30465
type Merge<F, S> = {
[K in keyof F | keyof S]:
K extends keyof F & keyof S
? S[K]
: K extends keyof F
? F[K]
: K extends keyof S
? S[K]
: never;
}
Solution by wouldRewind #30394
type Merge<F, S> = {[K in keyof S | keyof F]: K extends keyof S ? S[K]: K extends keyof F ? F[K] : never}
Solution by kai-phan #30366