// ============= 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] };
๋ค์ ๋ณต์กํ์ง๋ง ํ๋์ฉ ํ๋์ฉ ํ์ด์ ํด๊ฒฐํด๋ด ์๋ค.
/* _____________ ์ฌ๊ธฐ์ ์ฝ๋ ์
๋ ฅ _____________ */
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