01130-medium-replacekeys

Back

// 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