00043-easy-exclude

Back

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;

solution

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

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