๋ฐ์ํ
๋ฌธ์
T์์ U์ ํ ๋นํ ์ ์๋ ํ์ ์ ์ ์ธํ๋ ๋ด์ฅ ์ ๋ค๋ฆญ Exclude<T, U>๋ฅผ ์ด๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๊ตฌํํ์ธ์.
type Result = MyExclude<'a' | 'b' | 'c', 'a'> // 'b' | 'c'
ํ์ด
ํ์ ์ด ์๋ ๋จ์ ๋ฌธ์ ์๋ค๊ณ ์๊ฐํด๋ณด์
function MyExclude(T, U) {
if(T === U) return;
else return T;
}
์ด๋ฐ ๋๋์ด๋ค. T์ ํด๋นํ๋ ํ์ ๋ค์ค U์ ๊ฐ์ ํ์ ์ด ์๋ค๋ฉด ๊ทธ๊ฑด ๋ฐํํ์ง ์๊ณ , ๊ฐ์ง ์๋ค๋ฉด ๋ฐํํ๋ ๋ก์ง์ด๋ค.
ํ์ ์คํฌ๋ฆฝํธ์์ ===์ ํด๋นํ๋ ๋ถ๋ถ์ extends๋ก ๋์ฒด ๊ฐ๋ฅํ๊ณ
์๋ฌด๊ฒ๋ ๋ฐํํ์ง ์๋ ๊ฒฝ์ฐ๋ never ์ด๋ผ๋ ํ์ ์ผ๋ก ์ธ ์ ์๊ฒ ๋ค.
type MyExclude<T, U> = T extends U ? never : T;
์ถ์ฒ
๋ฐ์ํ
'๐ Typescript > type-challenges' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TS] type-challenges: 268 - If (0) | 2023.07.19 |
---|---|
[TS] type-challenges: 189 - Awaited (0) | 2023.07.17 |
[TS] type-challenges: 18. Length of Tuple (0) | 2023.07.05 |
[TS] type-challenges: 14. First of Array (0) | 2023.07.04 |
[TS] type-challenges : 11. Tuple to Object (0) | 2023.07.03 |