// your answers
type Combine<T> = {
[key in keyof T]: T[key]
}
type ReplaceKeys<U, T, Y> = U extends unknown
? Combine<{
[key in keyof U as key extends (T & keyof Y) ? never : key]: key extends T ? never: U[key]
} & {
[key in keyof Y as key extends (T & keyof U) ? key : never]: Y[key]
}>
: never;
Solution by AndreGeng #34751
type ReplaceKeys<U, T, Y> = {
[Key in keyof U]: Key extends T ? Key extends keyof Y ? Y[Key] : never : U[Key]
}
Solution by devshinthant #34626
type ReplaceKeys<U, T, Y> = {
[P in keyof U]: P extends T ? (P extends keyof Y ? Y[P] : never) : U[P]
}
Solution by ouzexi #34030
type ReplaceKeys<T, K,V> ={ [P in keyof T]: P extends K ? P extends keyof V ? V[P]: never : T[P] }
Solution by rookiewxy #33817
type ReplaceKeys<T, K, O> = T extends any ? {
[k in keyof T]: k extends K ? k extends keyof O ? O[k] : never : T[k]
} : never
Solution by Flavour86 #33507
// ไฝ ็็ญๆก
type ReplaceKeys<U, T, Y extends Record<any, any>> = U extends U ? {
[P in keyof U]: P extends T ? P extends keyof Y ? Y[P] : never : U[P]
}: U;
Solution by HelloGGG #33424
type ReplaceKeys<U, T, Y> = {
[k in keyof U]: k extends T ? Y[k & keyof Y] : U[k]
}
Solution by hui0808 #33422
type ReplaceKeys<U, T, Y> = {
[key in keyof U]: key extends T ?
key extends keyof Y ?
Y[key]
: never
: U[key];
}
Solution by ZhipengYang0605 #32984
type ReplaceKeys<U, T extends string, Y> = {
[K in keyof U] : K extends T ? (K extends keyof Y ? Y[K] : never) : U[K]
}
Solution by jinyoung234 #32464
type ReplaceKeys<U, T, Y> = U extends infer O
? {
[P in keyof O]: P extends T ? P extends keyof Y ? Y[P] : never : O[P]
}
: never```
Solution by keyurparalkar #32433
์ฒ์ ํ์ด
type ReplaceKeys<U, T, Y extends object> = U extends unknown
? { [K in keyof U]: K extends T | keyof Y ? K extends (T & keyof Y) ? Y[K] : never : U[K] }
: never
๋ ๊ฐ๊ฒฐํ ํ์ด
type ReplaceKeys<U, T, Y> = { [K in keyof U]: K extends T ? K extends keyof Y ? Y[K] : never : U[K] }
๋ถ๋ฐฐ๋ฒ์น์ ์ฌ์ฉํ๊ธฐ ์ํด์ ๋ฌด์กฐ๊ฑด ์ ์ผ๋ก U extends unknown์ ์ง์ด๋ฃ๋ ์ต๊ด์ ๊ณ ์ณ์ผํ๋ค
Solution by dev-hobin #32423
type ReplaceKeys<U extends Record<string, unknown>, T extends string, Y extends Record<string, unknown>> = {
[P in keyof U]: P extends T ? (P extends keyof Y ? Y[P] : never) : U[P];
};
Solution by gasmg #32069
// your answers
type ReplaceKeys<
U,
T extends PropertyKey,
Y extends Record<PropertyKey, any>
> = U extends any // ้ๅ union type
? {
// ๅฏน็ฎๆ ๅฏน่ฑก U ็ๆๆ key ๅบ็จๆไฝ
[key in keyof U]: key extends T // 1 key ๆฏไปฃๆฟๆข็ๅฑๆงไนไธๅ๏ผ
? key extends keyof Y // 1.true 1.1 payload Y ไธญ่ฟไธช key ๅ๏ผ
? Y[key] // 1.1.true ๅไฝฟ็จ Y ๆฅ่ฆ็
: never // 1.1.false Yใๆฒกๆใ่ฟไธช key, ๅ่ฎพไธบ never
: U[key]; // 1.false ๅฆๆ keyใไธๆฏใไปฃๆฟๆข็ๅฑๆงไนไธ๏ผไธๅๆฟๆข
}
: never;
Solution by wenxiangdong #31761
type ReplaceKeys<U, T, Y> = U extends infer V
? {[key in keyof V]: key extends T ? (key extends keyof Y ? Y[key] : never) : V[key]}
: never;
Solution by kai-phan #31671
type ReplaceKeys<U, T, Y> =
U extends U
? {
[K in keyof U]: K extends T
? K extends keyof Y
? Y[K]
: never
: U[K]
}
: never
Solution by jazelly #30784
type ReplaceKeys<U, T, Y> = U extends unknown
? Replace<U, T, Y>
: never;
type Replace<O, K, V> = {
[key in keyof O]: key extends K ? (key extends keyof V ? V[key] : never) : O[key]
}
Solution by kai-phan #30728
// Trigger distributed behavior
type ReplaceKeys<U, T, Y> = U extends U ? ReplaceOne<U, T, Y> : never
// Replace key
type ReplaceOne<O, T, Y> = {
[P in keyof O]: P extends T ? (P extends keyof Y ? Y[P] : never) : O[P]
}
Solution by Chan-Yuxi #30689
// your answers
type ReplaceKeys<TNode, TKeys, TMap> = {
[K in keyof TNode]: K extends TKeys ? K extends keyof TMap ? TMap[K] : never : TNode[K];
}
Solution by fero1xd #30295
Here is my solution
type ReplaceKeys<T, K, P extends Record<PropertyKey, any>> = T extends Record<string, any> ?
{ [k in keyof T as k extends K ? k : k]: k extends K ? k extends keyof P ? P[k] : never : T[k] } : never;
Solution by R11baka #30121
type ReplaceKeys<U, T, Y> = {[P in keyof U]: P extends T ? P extends keyof Y ? Y[P] : never : U[P]}
Solution by qianzhong516 #29951
type ReplaceKeys<U, T, Y> = {
[Key in keyof U]: Key extends T ?
Key extends keyof Y ? Y[Key] :
never :
U[Key]
}
Solution by maximallain #29787
type ReplaceKeys<U, T, Y> = {
[P in keyof U]: P extends T & keyof Y ? Y[P] : P extends T ? never : U[P]
}
Solution by Yirujet #29540
type ReplaceKeys<U, T, Y> = { [K in keyof U]: K extends T ? (K extends keyof Y ? Y[K] : never) : U[K]; };
Solution by DoubleWoodLin #28656
type ReplaceKeys<U, T extends PropertyKey, Y extends Record<PropertyKey, any>> = U extends U ? {
[K in keyof U]: K extends T ?
K extends keyof Y ? Y[K] : never
: U[K]
} : never;
Solution by jjswifty #27579
type ReplaceKeys<U, T extends string, Y> = {
[K in keyof U]: K extends T
? K extends keyof Y
? Y[K]
: never
: U[K]
}
Solution by HubooDeclan #27034
type ReplaceKeys<
U extends Record<any, any>,
T extends PropertyKey,
Y extends Record<any, any>,
P = { [K in keyof Pick<Y, T>]: K extends keyof Y ? Y[K] : never }
> = {
[K in keyof U]: K extends keyof P ? P[K] : U[K]
}
Solution by smileboyi #26868
type ReplaceKeys<U, T, Y> = {
[P in keyof U]: T extends keyof Y ?
P extends keyof Y ? Y[P] : U[P] :
P extends T ? never : U[P];
}
Solution by 8471919 #26736
type ReplaceKeys<U, T, Y> = { [key in keyof U]: key extends T ? key extends keyof Y ? Y[key] : never : U[key] }
Solution by AwesomeYelim #26647
// your answers
type ReplaceKeys<T, K extends string, V> = {
[key in keyof T]: key extends K ? key extends keyof V ? V[key] : never : T[key]
}
Solution by Hencky #26481
// your answers
type ReplaceKeys<U, T, Y> = U extends infer A ?
{
[K in keyof A]: K extends T
? K extends (keyof Y) ?
Y[K] : never
: A[K]
} : never
Solution by adoin #26167