00527-medium-append-to-object

Back

type AppendToObject<T, U extends string, V> = { [K in (keyof T | U)]: K extends keyof T ? T[K] : K extends U ? V : never }

Solution by eunsukimme #35237

type AppendToObject<T, U extends keyof unknown, V> = {
  [
    K in keyof T | U
  ]: K extends keyof T ? T[K] : V
};

Solution by devshinthant #34594

type AppendToObject<T, U extends PropertyKey, V> = {
  [P in keyof T | U]: P extends keyof T ? T[P] : V
}

Solution by ouzexi #34016

type AppendToObject<T extends {}, U extends string, V> = {
  [K in keyof T ]: T[K]
} & {
  [K in U]: V
}
image
// 需要将交叉类型合并
type Flatten<T extends {}> = { [K in keyof T] : T[K] }
type AppendToObject<T extends {}, U extends string, V> = Flatten<{
  [K in keyof T ]: T[K]
} & {
  [K in U]: V
}>
image

Solution by ScriptBloom #33911

// your answers

type AppendToObject<T extends Record<string, unknown>, U extends string, V extends unknown> = {
  [K in keyof T | U]: K extends keyof T ? T[K] : V
}

Solution by KeithChou #33246

type AppendToObject<T, U extends string, V, S = T & { [M in U]: V }> = {
  [K in keyof S]: S[K]
}

Solution by yukinotech #33130

type AppendToObject<
    T extends Record<keyof T, unknown>,
    K extends string,
    V extends unknown
> = T & Record<K, V>

type Test = { id: '1' }
type Result = AppendToObject<Test, 'value', 4> // expected to be { id: '1', value: 4 }

Solution by ZhulinskiiDanil #32699

type AppendToObject<T extends object, U extends keyof any, V> = { [K in (keyof T |  U)]: K extends keyof T ? T[K] : V }

Solution by dev-hobin #32404

type AppendToObject<T, U extends string, V> = {
  [P in keyof T | U]: (T & { [K in U]: V })[P];
};

Solution by jinyoung234 #31805

type AppendToObject<T, U extends PropertyKey, V> = {[key in keyof T | U]: key extends keyof T ? T[key] : V}

Solution by kai-phan #31643

type AppendToObject<T, U extends string, V> = { [P in (keyof T | U)]: P extends keyof T ? T[P]: V}

Solution by ivannovazzi #31555

type AppendToObject<T, U extends keyof any, V> = {
  [K in keyof T | U]: K extends keyof T ? T[K] : V;
};

Solution by vipulpathak113 #31476

type AppendToObject<T extends Record<string, any>, U extends string, V> = {
  [k2 in (keyof T | U)]: k2 extends U ? V: T[k2];
}

Solution by eward957 #31246

type AppendToObject<T extends Record<string, any>, U extends string, V> = Omit<{
  [K in keyof T]: T[K]
} & Record<U, V>, never>

Solution by Minato1123 #31095

type AppendToObject<T, U extends string, V> = {
  [P in keyof T | U] : P extends  keyof T ? T[P] : V
}

we can extension key,so use P in keyof T | U],tehn we use key to realize value

Solution by MrRabbit1993 #30463

type AppendToObject<T, U extends string, V> = {[K in keyof T | U]: K extends keyof T ? T[K] : V}

Solution by kai-phan #30354


type AppendToObject<T, U extends string | number | symbol, V> = {
  [K in keyof T | U]: K extends U ? V : K extends keyof T ? T[K] : never
}

type cases = [
  Expect<Equal<AppendToObject<{ k: number }, 'k', boolean>, { k: boolean }>>
]
----


Solution by zsc347 #30264

type Merge<T extends object> = {
  [P in keyof T]: T[P]
}

type AppendToObject<T extends object, U extends string, V> = Merge<
  {
    [P in keyof T]: T[P]
  } & Record<U, V>
>

Solution by agus-wesly #30252

type AppendToObject<T, K extends string, V> = {
  [key in keyof T | U] : key extends keyof T ? T[key] : V;
}

Solution by kanishev #30128

type Test = { id: '1' }

type AppendToObject<O extends {}, K extends Readonly<string>, V> = Keys<{
  [k in keyof O]: O[k]
} & { [k in K]: V }>;

type Keys<T> = {
  [k in keyof T]: T[k]
}

type Result = AppendToObject<Test, 'value', 4> // expected to be { id: '1', value: 4 }

Solution by rxMATTEO #30096

type AppendToObject<T, U extends string | symbol, V> = { [P in keyof T | U]: P extends keyof T ? T[P] : V; };

Solution by tasseles #29988

type AppendToObject<T, U extends string, V> = {
  [K in keyof T | U]: K extends keyof T ? T[K] : V;
}

Solution by maximallain #29563

// 여기 풀이를 입력하세요
type AppendToObject<T, U extends keyof any, V> = {[P in keyof T | U]: P extends  keyof T ? T[P] : V}

Solution by eodnjs467 #29426


type AppendToObject<T, U extends string, V> = 
  { 
    [K in keyof T]: T[K] 
  } & { [K in U]: V }

Solution by MohammadArasteh #29416

// 你的答案

type AppendToObject<T, U extends string, V> = { [key in keyof T]:T[key] } & { [K in U]: V }

Solution by kangaroona #29329

// 你的答案

type AppendToObject<T, U extends string, V> = { [key in keyof T]:T[key] } & { [K in U]: V }

Solution by kangaroona #29328

type Obj<T extends Record<string, any>> = {
    [P in keyof T]: T[P]
}

type AppendToObject<T extends Record<string, any>, U extends string, V> = Obj<{
    [P in keyof T]: T[P];
} & { [K in U]: V }>

Solution by Yirujet #29291

type AppendToObject<T, U, V> = {
  [key in keyof T | U]: key extends keyof T ? T[key] : V;
}

Solution by sabercc #29231

type AppendToObject<T, U extends PropertyKey, V> = {[P in U | keyof T ]: P extends keyof T ? T[P] : V}

Solution by qianzhong516 #28953

type AppendToObject<T, U extends PropertyKey, V> = {[K in U | keyof T]: K extends keyof T ? T[K] : K extends U ? V : never};

Solution by kai-phan #28906