๋ฐ˜์‘ํ˜•

๐Ÿ’™ Typescript/type-challenges 9

[TS] type-challenges: 268 - If

๋ฌธ์ œ ์กฐ๊ฑด `C`, ์ฐธ์ผ ๋•Œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํƒ€์ž… `T`, ๊ฑฐ์ง“์ผ ๋•Œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํƒ€์ž… `F`๋ฅผ ๋ฐ›๋Š” ํƒ€์ž… `If`๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”. `C`๋Š” `true` ๋˜๋Š” `false`์ด๊ณ , `T`์™€ `F`๋Š” ์•„๋ฌด ํƒ€์ž…์ž…๋‹ˆ๋‹ค. ์˜ˆ์‹œ: type A = If // expected to be 'a' type B = If // expected to be 'b' ํ’€์ด type If = C extends true ? T : F type์—์„œ extends๊ฐ€ ๋“ฑํ˜ธ ๋‘๊ฐœ == ์— ํ•ด๋‹นํ•œ๋‹ค๊ณ  ๋ณด๋ฉด ํŽธํ•  ๊ฒƒ ๊ฐ™๋‹ค ์ถœ์ฒ˜ https://github.com/type-challenges/type-challenges/blob/main/questions/00268-easy-if/README.ko.md

[TS] type-challenges: 189 - Awaited

๋ฌธ์ œ Promise์™€ ๊ฐ™์€ ํƒ€์ž…์— ๊ฐ์‹ธ์ธ ํƒ€์ž…์ด ์žˆ์„ ๋•Œ, ์•ˆ์— ๊ฐ์‹ธ์ธ ํƒ€์ž…์ด ๋ฌด์—‡์ธ์ง€ ์–ด๋–ป๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ์š”? ์˜ˆ์‹œ: ๋“ค์–ด Promise์ด ์žˆ์„ ๋•Œ, ExampleType์„ ์–ด๋–ป๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ์„๊นŒ์š”? type ExampleType = Promise type Result = MyAwaited // string ํ’€์ด type MyAwaited = T extends PromiseLike ? MyAwaited : T; Array vs ArrayLike, Promise vs PromiseLike ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—๋Š” ArrayLike๋ผ๋Š”๊ฒŒ ์กด์žฌํ•œ๋‹ค. Array๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด์„ ์˜๋ฏธํ•˜๋Š”๋ฐ, ArrayLike๋Š” ๋ฌด์—‡์ผ๊นŒ? ์ด๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด lib.es5.d.ts์— ๊ฐ€์„œ ๊ฐ๊ฐ์˜ ์ŠคํŽ™์„ ์‚ดํŽด๋ณด์ž. Array ArrayL..

[TS] type-challenges: 43. Exclude

๋ฌธ์ œ T์—์„œ U์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์„ ์ œ์™ธํ•˜๋Š” ๋‚ด์žฅ ์ œ๋„ค๋ฆญ Exclude๋ฅผ ์ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ตฌํ˜„ํ•˜์„ธ์š”. type Result = MyExclude // 'b' | 'c' ํ’€์ด ํƒ€์ž…์ด ์•„๋‹Œ ๋‹จ์ˆœ ๋ฌธ์ œ์˜€๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž function MyExclude(T, U) { if(T === U) return; else return T; } ์ด๋Ÿฐ ๋Š๋‚Œ์ด๋‹ค. T์— ํ•ด๋‹นํ•˜๋Š” ํƒ€์ž…๋“ค์ค‘ U์™€ ๊ฐ™์€ ํƒ€์ž…์ด ์žˆ๋‹ค๋ฉด ๊ทธ๊ฑด ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ , ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด ๋ฐ˜ํ™˜ํ•˜๋Š” ๋กœ์ง์ด๋‹ค. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ ===์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„์€ extends๋กœ ๋Œ€์ฒด ๊ฐ€๋Šฅํ•˜๊ณ  ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” never ์ด๋ผ๋Š” ํƒ€์ž…์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๊ฒ ๋‹ค. type MyExclude = T extends U ? never : T; ์ถœ์ฒ˜ https://github.com..

[TS] type-challenges: 18. Length of Tuple

๋ฌธ์ œ ๋ฐฐ์—ด(ํŠœํ”Œ)์„ ๋ฐ›์•„ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ œ๋„ค๋ฆญ Length๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”. type tesla = ['tesla', 'model 3', 'model X', 'model Y'] type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT'] type teslaLength = Length // expected 4 type spaceXLength = Length // expected 5 ํ’€์ด type Length = T['length']; ๊ฐ„๋‹จํ•˜๊ฒŒ ์ด๋ ‡๊ฒŒ ํ‘œํ˜„ํ–ˆ๋”๋‹ˆ Type 'readonly ["tesla", "model 3", "model X", "model Y"]' does not satisfy the constrai..

[TS] type-challenges: 14. First of Array

๋ฌธ์ œ ๋ฐฐ์—ด(ํŠœํ”Œ) T๋ฅผ ๋ฐ›์•„ ์ฒซ ์›์†Œ์˜ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ œ๋„ค๋ฆญ First๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”. type arr1 = ['a', 'b', 'c'] type arr2 = [3, 2, 1] type head1 = First // expected to be 'a' type head2 = First // expected to be 3 ํ’€์ด ๋น„๊ต์  ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๋ผ์„œ T[0]์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ–ˆ๋Š”๋ฐ, ๊ทธ๋žฌ๋”๋‹ˆ []์ธ๊ฒฝ์šฐ never์— ๋Œ€ํ•œ ์ผ€์ด์Šค์— ๊ฑธ๋ ค์„œ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด์คฌ๋‹ค type First = T extends [] ? never : T[0]; ์ถœ์ฒ˜ https://github.com/type-challenges/type-challenges/blob/main/questions/00014-easy-first/README.ko.m..

[TS] type-challenges : 11. Tuple to Object

๋ฌธ์ œ ๋ฐฐ์—ด(ํŠœํ”Œ)์„ ๋ฐ›์•„, ๊ฐ ์›์†Œ์˜ ๊ฐ’์„ key/value๋กœ ๊ฐ–๋Š” ์˜ค๋ธŒ์ ํŠธ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํƒ€์ž…์„ ๊ตฌํ˜„ํ•˜์„ธ์š”. ์˜ˆ์‹œ const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const type result = TupleToObject // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'} ํ’€์ด ๊ฐ์ฒด์˜ ํ‚ค๋„, ๊ฐ’๋„ T๋ฐฐ์—ด์˜ ์š”์†Œ๊ธฐ ๋•Œ๋ฌธ์— ํ‚ค๊ฐ’ kv๊ฐ€ T[number]์— ์†ํ•ด์žˆ๊ณ  ์ด ๊ฒƒ์ด ํ‚ค์ธ ๋™์‹œ์— ๊ฐ’์ด๋ผ๊ณ  ๋ช…์‹œํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ๋œ๋‹ค. ์ด๋•Œ T๋Š” string ๋˜๋Š” number ๊ฐ€ ํฌํ•จ๋œ ๋ฐฐ์—ด์ด๋ผ๋Š” ์ ๋„ ๋ช…์‹œํ•ด์ค€๋‹ค. type TupleToObject..

[TS] type-challenges : 7. Readonly

๋ฌธ์ œ T์˜ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฝ๊ธฐ ์ „์šฉ(์žฌํ• ๋‹น ๋ถˆ๊ฐ€)์œผ๋กœ ๋ฐ”๊พธ๋Š” ๋‚ด์žฅ ์ œ๋„ค๋ฆญ Readonly๋ฅผ ์ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ตฌํ˜„ํ•˜์„ธ์š”. ์˜ˆ์‹œ: interface Todo { title: string description: string } const todo: MyReadonly = { title: "Hey", description: "foobar" } todo.title = "Hello" // Error: cannot reassign a readonly property todo.description = "barFoo" // Error: cannot reassign a readonly property ํ’€์ด type MyReadonly = {readonly [key in keyof T]: T[key]} ์ถœ์ฒ˜ https..

[TS] type-challenges : 4. Pick

๋ฌธ์ œ T์—์„œ K ํ”„๋กœํผํ‹ฐ๋งŒ ์„ ํƒํ•ด ์ƒˆ๋กœ์šด ์˜ค๋ธŒ์ ํŠธ ํƒ€์ž…์„ ๋งŒ๋“œ๋Š” ๋‚ด์žฅ ์ œ๋„ค๋ฆญ Pick์„ ์ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ตฌํ˜„ํ•˜์„ธ์š”. ์˜ˆ์‹œ: interface Todo { title: string description: string completed: boolean } type TodoPreview = MyPick const todo: TodoPreview = { title: 'Clean room', completed: false, } ํ’€์ด type MyPick = {[key in K]: T[key]}; ์ถœ์ฒ˜ https://github.com/type-challenges/type-challenges/blob/main/questions/00004-easy-pick/README.ko.md GitHub - type-chal..

[TS] type-challenges : 13. Hello World

๋ฌธ์ œ Hello, World! Type Challenges์—์„œ๋Š” ํƒ€์ž… ๋‹จ์–ธ(assertion)์„ ํ•˜๊ธฐ ์œ„ํ•ด ์ž์ฒด์ ์ธ ํƒ€์ž… ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ œ์—์„œ๋Š”, ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ด์„œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ํ†ต๊ณผํ•˜์„ธ์š”. (ํƒ€์ž… ์ฒดํฌ ์—๋Ÿฌ ์—†์Œ). // string์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. type HelloWorld = any // ์•„๋ž˜์˜ ํ…Œ์ŠคํŠธ๊ฐ€ ํ†ต๊ณผํ•˜๋„๋ก ๋งŒ๋“œ์„ธ์š”. type test = Expect Take the Challenge ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด์„œ ์ฝ”๋”ฉ์„ ์‹œ์ž‘ํ•˜์„ธ์š”! Happy Hacking! ํ’€์ด /* _____________ ์—ฌ๊ธฐ์— ์ฝ”๋“œ ์ž…๋ ฅ _____________ */ type HelloWorld = string // expected to be a string /* _____________ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ___..

๋ฐ˜์‘ํ˜•