๐Ÿ’™ Typescript/type-challenges

[TS] type-challenges: 268 - If

์„ ๋‹ฌ 2023. 7. 19. 17:35
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

์กฐ๊ฑด `C`, ์ฐธ์ผ ๋•Œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํƒ€์ž… `T`, ๊ฑฐ์ง“์ผ ๋•Œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํƒ€์ž… `F`๋ฅผ ๋ฐ›๋Š” ํƒ€์ž… `If`๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”. `C`๋Š” `true` ๋˜๋Š” `false`์ด๊ณ , `T`์™€ `F`๋Š” ์•„๋ฌด ํƒ€์ž…์ž…๋‹ˆ๋‹ค.

  ์˜ˆ์‹œ:

  type A = If<true, 'a', 'b'>  // expected to be 'a'
  type B = If<false, 'a', 'b'> // expected to be 'b'

 

ํ’€์ด

type If<C extends boolean, T, F> = C extends true ? T : F

 

type์—์„œ extends๊ฐ€ ๋“ฑํ˜ธ ๋‘๊ฐœ == ์— ํ•ด๋‹นํ•œ๋‹ค๊ณ  ๋ณด๋ฉด ํŽธํ•  ๊ฒƒ ๊ฐ™๋‹ค

 

์ถœ์ฒ˜

https://github.com/type-challenges/type-challenges/blob/main/questions/00268-easy-if/README.ko.md

 

๋ฐ˜์‘ํ˜•

'๐Ÿ’™ Typescript > type-challenges' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[TS] type-challenges: 189 - Awaited  (0) 2023.07.17
[TS] type-challenges: 43. Exclude  (0) 2023.07.14
[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