00003-medium-omit

Back

// ============= Test Cases =============
import type { Equal, Expect } from './test-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 {
  title: string
  description: string
  completed: boolean
}

interface Expected1 {
  title: string
  completed: boolean
}

interface Expected2 {
  title: string
}


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

Solution by hawkWang98 #35507

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

Solution by gangnamssal #35497

type MyReadonly2<T, K extends keyof T = keyof T> = {readonly [key1 in K] : T[key1]} & {
  [key2 in keyof T as key2 extends K ?  never : key2] : T[key2]
}

Solution by RanungPark #35448

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

Solution by RanungPark #35447

// your answers
interface Todo {
    title: string;
    description: string;
    completed: boolean;
}

type MyOmit<T extends {}, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;

const myTodo: MyOmit<Todo, "title" | "description"> = {
    completed: false,
};

Solution by Sathiyapramod #35409

// your answers
type MyOmit<T, K> = Pick<T, Exclude<keyof T, K>>;

Solution by kdh3543 #35395

type MyOmit<T, K extends keyof T> = Omit<T,K>

Solution by TaueFenCheng #35329

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

Solution by yujun96 #35284

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

Solution by wendao-liu #35158

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

Solution by chenweiHuabi #35100

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

Solution by ClarityOfMind #35039

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

Solution by raeyoung-kim #34995

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

Solution by raeyoung-kim #34986

type MyOmit<T, K extends keyof T> = Omit<T,K>

Solution by Niravkhetani #34983

// your answers

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

Solution by 2njeong #34965

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

// your answers

Solution by Xiaofan-song #34959

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

why it doesn't work?

Solution by shx123qwe #34911

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

Solution by eunsukimme #34820

// your answers
type MyOmit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

Solution by zeyuanHong0 #34752

// ์—ฌ๊ธฐ ํ’€์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
type MyOmit<T, K extends keyof T> = {
  [P in keyof T as P extends K ? never : P]: T[P];
};

Solution by LeeKangHyun #34587

type MyOmit<T extends Record<string, any>, K extends keyof T> = {  [P in keyof T as (P extends K ? never : P)]: T[P] }
// your answers

Solution by Rustamaha #34586

We should check K's extends condition(keyof T) to pass @ts-expect-error. And, use as to restrict "not" extends K condition.

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

Solution by dev-jaemin #34584

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

Solution by Yunjaejo #34580

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

Solution by bkdragon0228 #34568

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

Solution by devshinthant #34553

type Exclude<T, U> = T extends U ? never : T;

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

Solution by wxh-cyber #34439

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

Solution by ktim816 #34417

๋ฌธ์ œ ์„ค๋ช…

ํ’€์ด

type MyOmit<T, K> = T extends K ? never : T;

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜์˜€์œผ๋‚˜ ์ด๊ฑด Excludes ์ฒ˜๋Ÿผ์˜ ๋™์ž‘๊ณผ ๊ฐ™์•„์„œ ๋ฐฐ์ œํ•ฉ๋‹ˆ๋‹ค. ์˜คํžˆ๋ ค Pick๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด์•ผํ–ˆ์Šต๋‹ˆ๋‹ค.

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

๋‹ค์†Œ ๋ณต์žกํ•˜์ง€๋งŒ ํ•˜๋‚˜์”ฉ ํ•˜๋‚˜์”ฉ ํ’€์–ด์„œ ํ•ด๊ฒฐํ•ด๋ด…์‹œ๋‹ค.

  1. keyof T๋Š” ๊ฐ์ฒด ํƒ€์ž… T์˜ ๋ชจ๋“  ํ‚ค๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” TypeScript์˜ ๋‚ด์žฅ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.
  2. [key in keyof T]๋Š” T์˜ ๋ชจ๋“  ํ‚ค๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ, ์ƒˆ๋กœ์šด ๊ฐ์ฒด ํƒ€์ž…์„ ๋งŒ๋“œ๋Š” ๊ตฌ๋ฌธ์ž…๋‹ˆ๋‹ค.
  3. key extends K ? never : key ๋Š” key๊ฐ€ K์— ํฌํ•จ๋˜๋Š”์ง€ (extends K)๋ฅผ ๊ฒ€์‚ฌํ•˜๊ณ , ๋งŒ์•ฝ ํฌํ•จ๋œ๋‹ค๋ฉด never๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด key๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Solution

/* _____________ ์—ฌ๊ธฐ์— ์ฝ”๋“œ ์ž…๋ ฅ _____________ */

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

/* _____________ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค _____________ */
import type { Equal, Expect } from "@type-challenges/utils";

type cases = [
	Expect<Equal<Expected1, MyOmit<Todo, "description">>>,
	Expect<Equal<Expected2, MyOmit<Todo, "description" | "completed">>>,
	Expect<Equal<Expected3, MyOmit<Todo1, "description" | "completed">>>
];

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

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

interface Todo1 {
	readonly title: string;
	description: string;
	completed: boolean;
}

interface Expected1 {
	title: string;
	completed: boolean;
}

interface Expected2 {
	title: string;
}

interface Expected3 {
	readonly title: string;
}

Solution by adultlee #34127

type Exclude<TKey, KKey> = TKey extends KKey ? never : TKey;

type MyPick<T, K extends keyof T> = {
	[TK in K]: T[TK];
};

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

Solution by ProvorovOleksii #34099

// ไฝ ็š„็ญ”ๆกˆ
type Key<TK, K extends TK> = TK extends K ? never : TK;
type MyOmit<T, K extends keyof T> = {
  [P in Key<keyof T, K>]: T[P];
};

Solution by yyl134934 #34081