type Exclude<T extends unknown, U extends T | PropertyKey | unknown = T> = T extends U ? never : T;
type MixedTypes = 'apple' | 'banana' | 1 | 2 | (() => void) | { key: string } | false;
type Test1 = Exclude<MixedTypes, 'apple' | 'banana'>; // 'apple' | 'orange'
type Test2 = Exclude<MixedTypes, 1 | 2>; // 'apple' | 'banana' | (() => void) | { key: string } | false
type Test3 = Exclude<MixedTypes, false>; // 'apple' | 'banana' | 1 | 2 | (() => void) | { key: string }
type Test4 = Exclude<MixedTypes, (() => void) | { key: string }>; // 'apple' | 'banana' | 1 | 2 | false
type Test5 = Exclude<MixedTypes, string> // false | {key: string;} | 1 | 2 | (() => void)
type Test6 = Exclude<MixedTypes, string | number> // false | {key: string;} | (() => void)
type Test7 = Exclude<MixedTypes, string | number | boolean> // {key: string;} | (() => void)
type Test8 = Exclude<MixedTypes, Function> // {key: string;}
Solution by w3Scribe #35198
type MyExclude<T, U> = T extends U ? never : T;
Solution by raeyoung-kim #34938
type MyExclude<T, U> = T extends U ? never : T
Solution by 56aiden90 #34879
type MyExclude<T, U extends Partial<T>> = T extends U ? never : T
Solution by nguyste #34854
type MyExclude<T,U> = T extends U ? never : T
Solution by dan0314 #34841
type MyExclude<T, U> = T extends U ? never : T
Solution by eunsukimme #34811
type MyExclude<T, U> = T extends U ? never : T
Solution by kang-kibong #34745
type MyExclude<T, U> = T extends U ? never : T
Solution by Git-I985 #34701
// your answers
type MyExclude<T, U> = T extends U ? never : T
Solution by zeyuanHong0 #34690
type MyExclude<T, U> = T extends U ? never : T;
Solution by nathan2slime #34658
type MyExclude<T, U> = T extends U ? never : T
Solution by Hailey0930 #34638
type MyExclude<T, U> = T extends U ? never : T
Solution by devshinthant #34543
// your answers
type MyExclude<T, U> = T extends U ? never : T;
Solution by LeeKangHyun #34455
type MyExclude<T, U> = T extends U ? never : T;
Solution by ktim816 #34422
type MyExclude<T, U> = T extends U ? never : T
Solution by binhdv155127 #34339
type MyExclude<T, U> = T extends U ? never : T;
ๆณจๆTSๅฏน่ๅ็ฑปๅไผๆง่กๅ้ ็
type C=Exclude<'a'|'b','a'|'c'> //'b'
//็ญไปทไบ
Exclude<'a','a'|'c'>|Exclude<'b','a'|'c'>
Solution by wxh-cyber #34334
type MyExclude<T, U> = T extends U ? never : T;
Solution by bkdragon0228 #34273
type MyExclude<T, U> = T extends U ? never : T
Solution by quitone #34191
Extract K from T, then we can check that K extends U
.
type MyExclude<T, U> = T extends infer K ? (K extends U ? never : K) : never
Solution by dev-jaemin #34158
T
์์ U
์ ํ ๋นํ ์ ์๋ ํ์
์ ์ ์ธํ๋ ๋ด์ฅ ์ ๋ค๋ฆญ Exclude<T, U>
๋ฅผ ์ด๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๊ตฌํํ์ธ์.
์์:
type Result = MyExclude<"a" | "b" | "c", "a">; // 'b' | 'c'
ํด๋น ๋ฌธ์ ๋ ์กฐ๊ฑด๋ฌธ ํ์ ๋ด์์์ extends์ ๋ํด์ ์ดํดํ๋ฉด ์ฝ๊ฒ ํ์ดํ ์ ์์ต๋๋ค.
์กฐ๊ฑด๋ถ ํ์ ์ T extends U ? X : Y์ ํํ๋ฅผ ๊ฐ์ง๋ฉฐ, ์ด๋ T๊ฐ U์ ํ ๋น ๊ฐ๋ฅํ๋ฉด X ํ์ ์ ๋ฐํํ๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉด Y ํ์ ์ ๋ฐํํ๋ ํ์ ์ ๋๋ค. (์ฌ๊ธฐ์ T ํน์ U๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ค๋ฉด, ํด๋น ํ์ธ ๋จ๊ณ์ ํฌํจํ ์ธ์๋ฅผ ๋ฃ๊ฒ ๋ฉ๋๋ค. for in ๋ฌธ๊ณผ ์ ์ฌํฉ๋๋ค.)
type MyExclude<T, U> = T extends U ? never : T;
/* _____________ ์ฌ๊ธฐ์ ์ฝ๋ ์
๋ ฅ _____________ */
type MyExclude<T, U> = T extends U ? never : T;
/* _____________ ํ
์คํธ ์ผ์ด์ค _____________ */
import type { Equal, Expect } from "@type-challenges/utils";
type cases = [
Expect<Equal<MyExclude<"a" | "b" | "c", "a">, "b" | "c">>,
Expect<Equal<MyExclude<"a" | "b" | "c", "a" | "b">, "c">>,
Expect<
Equal<MyExclude<string | number | (() => void), Function>, string | number>
>
];
Solution by adultlee #34122
type MyExclude<T, U extends any> = T extends U ? never : T;
Solution by ProvorovOleksii #34078
type MyExclude<T, U> = T extends U ? never : T
Solution by ouzexi #33967
type MyExclude<T, U> = T extends U ? never : T
Solution by iam1maker #33929
type MyExclude<T, U> = T extends U ? never : T
Solution by notsecret32 #33859
type MyExclude<T, U> = T extends U ? never : T;
Solution by veralex #33749
type MyExclude<T, U> = T extends U?never:T
Solution by d0422 #33732
type MyExclude<T, U> = T extends U ? never : T
Solution by laplace1009 #33701
type MyExclude<T, K> = T extends K ? never : T
extends
ใงๆกไปถๅๅฒใใใฆnever
่ฟใใฎใฏๆใใคใใชใใฃใ...
Solution by okabe-yuya #33647
type MyExclude<T, U> = Exclude<T,U>
Solution by okasyun #33629
type MyExclude<T, U> = T extends U ? never : T;
Solution by oo009pbh #33621