00003-medium-omit

Back

// 你的答案

type MyOmit<T, K extends keyof T> = { [key in keyof T as Exclude<key, K>]: T[key] }改了几分钟发现是keyof T的keyof 没加,我觉得既然只限制Omit,那么其他也没说不可以使用,所以投机取巧了下,或者就是楼上的答案。

Solution by shx123qwe #33574

 type MyOmit<T,K extends keyof T> = {
    [ P in keyof T as  P extends K ? never : P ]: T[P]
}  

Solution by yuzhou-tang #33573

type MyOmit<T, K extends keyof T> = {
  [Prop in keyof T as Prop extends K ? never : Prop]: T[Prop]
}```

Solution by Kolufs #33534

// 여기 풀이를 입력하세요
type MyExclude<T, U> = T extends U ? never : T;
type MyPick<T, K extends keyof T> = { [P in K]: T[P] };

type MyOmit<T, K extends keyof T> = MyPick<T, MyExclude<keyof T, K>>;

Solution by awesomelon #33388

// 你的答案
type MyOmit<T, U extends keyof T> = {
  [key in keyof T as key extends U ? never : key]: T[key];
};

Solution by 2531800823 #33237

type MyOmit<T, K extends keyof T> = {
  [P in keyof T as P extends K ? never : P]: T[P];
}

Solution by loevray #33205

type MyOmit<T, U extends keyof T> = {
  [K in keyof T as K extends U ? never : K]: T[K]
}

Solution by Tubring25 #33151

type MyOmit<T, K> = {[Key in keyof T as Key extends K ? never : Key]: T[Key]}

Solution by ry928330 #33132

首先Omit<A, B>可以理解为Pick<A, C>,其中B | C exends keyof A。例如A = {a:1, b: 2, c: 3, d: 4} B = 'a'|'b' 那么C = 'c'|'d' type MyOmit<A, B extends keyof A> = type Pick<A, Exclude<keyof A, B>> Pick和Exclude也可以自定义实现 type MyPick<A, B extends keyof A> = {[K in B]: A[K]} type MyExclude<A, B> = A extends B ? never : A

// 你的答案

Solution by DwayneDuanJY #33068

type MyOmit<T, K extends keyof T> = { [key in keyof T as key extends K ? never : key ]: T[key] }

Solution by RanungPark #33061

type MyOmit<T, K> = Pick<T, Exclude<keyof T, K>>

Solution by Coeur101 #32890

// 主要还是关键字的理解
type MyOmit<T, K extends keyof T> = {
  [P in keyof T as P extends K ? never : P]:T[P]
}

Solution by CAN1177 #32853

// 解答をここに記入
type MyOmit<T extends object,K extends keyof T> = {
  [Key in Exclude<keyof T,K>]: T[Key]
}

Solution by Yasunori-aloha #32802

type MyOmit<T, K extends keyof T> = {
  [key in keyof T as key extends K ? never : key]: T[key];
}

Solution by ZhipengYang0605 #32796

type MyOmit<T, K extends keyof T> = {
  [p in keyof T as (p extends K ? never : p)]: T[p];
}

// or
type MyOmit<T, K extends keyof T> = {
  [
    p in keyof T as (
      p extends K 
          ? never 
          : p
      )
  ]: T[p];
}

Solution by mistkafka #32632

// your answers
type Exclude<T,K> = T extends K ? never : T; 

type MyOmit<T, K> = {
  [ key in Exclude<keyof T,K>]: T[key]  
}

Solution by Hujianboo #32597

// keep readonly declare
type MyOmit<T, K extends keyof T> = {
  [P in keyof T as P extends K ? never : P]: T[P]
};

// for example
/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Expected1, MyOmit<Todo, 'description'>>>,
  Expect<Equal<Expected2, MyOmit<Todo, 'description' | 'completed'>>>,
]

// @ts-expect-error
type error = MyOmit<Todo, 'description' | 'invalid'>

interface Todo {
  readonly title: string // add readonly
  description: string
  completed: boolean
}

interface Expected1 {
  readonly title: string // add readonly
  completed: boolean
}

interface Expected2 {
  readonly title: string // add readonly
}

Solution by xachary #32549

 interface Todo {
    title: string
    description: string
    completed: boolean
}

type OmitPreview = MyOmit<Todo, 'description' | 'title'>

const Omit: OmitPreview = {
    completed: false,
}

type MyOmit<T, K extends keyof T> = {
    [P in Exclude<keyof T, K>]: T[P]
}

Solution by sugar258596 #32538

// 你的答案
type MyOmit<T, K extends keyof T> = {
    [P in keyof T as P extends K ? never: P]: T[P]
}

Solution by Jabo2017 #32507

type MyOmit<T, K extends keyof T> = { [key in keyof T as key extends K ? never : key]: T[key] }

Solution by jcyicai #32454

type MyOmit<T, K> = {
  [key in Exclude<keyof T, K>]: T[key]
}

Solution by zhengpq #32451

type MyOmit<T, K extends keyof T> = { [P in Exclude<keyof T, K>]: T[P] }

Solution by laqudee #32319

type MyOmit<T, K> = {
  [P in keyof T as Exclude<P, K>]: T[P]
}

Solution by keyurparalkar #32315

  1. Use built-in Exclude:
type MyOmit<T, K extends keyof T> =  {[key in Exclude<keyof T, K>]: T[key]};
  1. Use as condition filter
type MyOmit<T, K extends keyof T> =  {[key in keyof T as key extends K ? never: key]: T[key]};

Solution by Enzo1994 #32176

Exclude 사용

type MyOmit<T, K extends keyof T> = { [P in Exclude<keyof T, K>]: T[P] }

as 절 사용 (key-remapping-in-mapped-types)

type MyOmit<T, K extends keyof T> = { [P in keyof T as P extends K ? never: P] :T[P] }

Solution by dev-hobin #32167

type MyOmit<T, K extends keyof T> = Pick<
  T,
  Exclude<keyof T, K>
>

Firstly, we omit all those keys that are in K from the keys of T using Exclude<keyof T, K> Then, with those keys we picks the properties from T we need using Pick

Solution by joyanedel #32137

// your answers
type MyOmit<T extends Record<string, any>, K extends keyof T> = { [P in Exclude<keyof T, K>]: T[P] }

Solution by Rustamaha #32074

interface Todo { title: string description: string completed: boolean }

type MyOmit<T, K extends keyof T> = {[P in keyof T as P extends K ? never: P] :T[P]}

type TodoPreview = MyOmit<Todo, 'description' | 'title'>

const todo: TodoPreview = { completed: false, }

Solution by rkamely #32051

type MyOmit<T, K> = {
  [Properties in keyof T as Properties extends K ? never : Properties]: T[Properties]
}

Solution by trinhvinhtruong96 #32022

// 你的答案
type MyOmit<T, U extends keyof T> = Pick<T, Exclude<keyof T, U>>

Solution by ruofee #32012