[Typescript] 38. Medium - Diff

Get an Object that is the difference between O & O1

/* _____________ Your Code Here _____________ */

type Diff<T, S> = {
  [K in Exclude<(keyof T | keyof S), (keyof T & keyof S)>]: (T & S)[K]
}

// Exclude<(keyof Foo | keyof Coo), (keyof Foo & keyof Coo)> = 'age' | 'gender', only get diff

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type Foo = {
  name: string
  age: string
}
type Bar = {
  name: string
  age: string
  gender: number
}
type Coo = {
  name: string
  gender: number
}

type cases = [
  Expect<Equal<Diff<Foo, Bar>, { gender: number }>>,
  Expect<Equal<Diff<Bar, Foo>, { gender: number }>>,
  Expect<Equal<Diff<Foo, Coo>, { age: string; gender: number }>>,
  Expect<Equal<Diff<Coo, Foo>, { age: string; gender: number }>>,
]

 

posted @ 2022-09-27 01:47  Zhentiw  阅读(20)  评论(0编辑  收藏  举报