Skip to content

Commit 86780b5

Browse files
committed
[3주차] challenges(easy)
1 parent a1d83c4 commit 86780b5

File tree

12 files changed

+555
-0
lines changed

12 files changed

+555
-0
lines changed

백윤서/practice/11.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
11 - Tuple to Object
3+
-------
4+
by sinoon (@sinoon) #쉬움 #object-keys
5+
6+
### 질문
7+
8+
배열(튜플)을 받아, 각 원소의 값을 key/value로 갖는 오브젝트 타입을 반환하는 타입을 구현하세요.
9+
10+
예시:
11+
12+
```ts
13+
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const
14+
15+
type result = TupleToObject<typeof tuple> // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}
16+
```
17+
18+
> GitHub에서 보기: https://tsch.js.org/11/ko
19+
*/
20+
21+
/* _____________ 여기에 코드 입력 _____________ */
22+
23+
type TupleToObject<T extends readonly PropertyKey[]> = {
24+
[key in T[number]]: key;
25+
}
26+
27+
/* _____________ 테스트 케이스 _____________ */
28+
import type { Equal, Expect } from '@type-challenges/utils'
29+
30+
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const
31+
const tupleNumber = [1, 2, 3, 4] as const
32+
const sym1 = Symbol(1)
33+
const sym2 = Symbol(2)
34+
const tupleSymbol = [sym1, sym2] as const
35+
const tupleMix = [1, '2', 3, '4', sym1] as const
36+
37+
type cases = [
38+
Expect<Equal<TupleToObject<typeof tuple>, { tesla: 'tesla'; 'model 3': 'model 3'; 'model X': 'model X'; 'model Y': 'model Y' }>>,
39+
Expect<Equal<TupleToObject<typeof tupleNumber>, { 1: 1; 2: 2; 3: 3; 4: 4 }>>,
40+
Expect<Equal<TupleToObject<typeof tupleSymbol>, { [sym1]: typeof sym1;[sym2]: typeof sym2 }>>,
41+
Expect<Equal<TupleToObject<typeof tupleMix>, { 1: 1; '2': '2'; 3: 3; '4': '4';[sym1]: typeof sym1 }>>,
42+
]
43+
44+
// @ts-expect-error
45+
type error = TupleToObject<[[1, 2], {}]>
46+
47+
/* _____________ 다음 단계 _____________ */
48+
/*
49+
> 정답 공유하기: https://tsch.js.org/11/answer/ko
50+
> 정답 보기: https://tsch.js.org/11/solutions
51+
> 다른 문제들: https://tsch.js.org/ko
52+
*/

백윤서/practice/13.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
13 - Hello World
3+
-------
4+
by Anthony Fu (@antfu) #워밍업
5+
6+
### 질문
7+
8+
Hello, World!
9+
10+
Type Challenges에서는 타입 단언(assertion)을 하기 위해 자체적인 타입 시스템을 사용합니다.
11+
12+
이 과제에서는, 아래의 코드를 변경해서 테스트 코드를 통과하세요. (타입 체크 에러 없음).
13+
14+
```ts
15+
// string이 되어야 합니다.
16+
type HelloWorld = any
17+
```
18+
19+
```ts
20+
// 아래의 테스트가 통과하도록 만드세요.
21+
type test = Expect<Equal<HelloWorld, string>>
22+
```
23+
24+
`Take the Challenge` 버튼을 클릭해서 코딩을 시작하세요! Happy Hacking!
25+
26+
> GitHub에서 보기: https://tsch.js.org/13/ko
27+
*/
28+
29+
/* _____________ 여기에 코드 입력 _____________ */
30+
31+
type HelloWorld = string // expected to be a string
32+
33+
/* _____________ 테스트 케이스 _____________ */
34+
import type { Equal, Expect, NotAny } from '@type-challenges/utils'
35+
36+
type cases = [
37+
Expect<NotAny<HelloWorld>>,
38+
Expect<Equal<HelloWorld, string>>,
39+
]
40+
41+
/* _____________ 다음 단계 _____________ */
42+
/*
43+
> 정답 공유하기: https://tsch.js.org/13/answer/ko
44+
> 정답 보기: https://tsch.js.org/13/solutions
45+
> 다른 문제들: https://tsch.js.org/ko
46+
*/

백윤서/practice/14.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
14 - First of Array
3+
-------
4+
by Anthony Fu (@antfu) #쉬움 #array
5+
6+
### 질문
7+
8+
배열(튜플) `T`를 받아 첫 원소의 타입을 반환하는 제네릭 `First<T>`를 구현하세요.
9+
10+
예시:
11+
12+
```ts
13+
type arr1 = ['a', 'b', 'c']
14+
type arr2 = [3, 2, 1]
15+
16+
type head1 = First<arr1> // expected to be 'a'
17+
type head2 = First<arr2> // expected to be 3
18+
```
19+
20+
> GitHub에서 보기: https://tsch.js.org/14/ko
21+
*/
22+
23+
/* _____________ 여기에 코드 입력 _____________ */
24+
25+
type First<T extends any[]> = T extends [] ? never : T[0]
26+
27+
/* _____________ 테스트 케이스 _____________ */
28+
import type { Equal, Expect } from '@type-challenges/utils'
29+
30+
type cases = [
31+
Expect<Equal<First<[3, 2, 1]>, 3>>,
32+
Expect<Equal<First<[() => 123, { a: string }]>, () => 123>>,
33+
Expect<Equal<First<[]>, never>>,
34+
Expect<Equal<First<[undefined]>, undefined>>,
35+
]
36+
37+
type errors = [
38+
// @ts-expect-error
39+
First<'notArray'>,
40+
// @ts-expect-error
41+
First<{ 0: 'arrayLike' }>,
42+
]
43+
44+
/* _____________ 다음 단계 _____________ */
45+
/*
46+
> 정답 공유하기: https://tsch.js.org/14/answer/ko
47+
> 정답 보기: https://tsch.js.org/14/solutions
48+
> 다른 문제들: https://tsch.js.org/ko
49+
*/

백윤서/practice/18.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
18 - Length of Tuple
3+
-------
4+
by sinoon (@sinoon) #쉬움 #tuple
5+
6+
### 질문
7+
8+
배열(튜플)을 받아 길이를 반환하는 제네릭 `Length<T>`를 구현하세요.
9+
10+
예시:
11+
12+
```ts
13+
type tesla = ['tesla', 'model 3', 'model X', 'model Y']
14+
type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT']
15+
16+
type teslaLength = Length<tesla> // expected 4
17+
type spaceXLength = Length<spaceX> // expected 5
18+
```
19+
20+
> GitHub에서 보기: https://tsch.js.org/18/ko
21+
*/
22+
23+
/* _____________ 여기에 코드 입력 _____________ */
24+
25+
type Length<T extends readonly any[]> = T['length']
26+
27+
/* _____________ 테스트 케이스 _____________ */
28+
import type { Equal, Expect } from '@type-challenges/utils'
29+
30+
const tesla = ['tesla', 'model 3', 'model X', 'model Y'] as const
31+
const spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT'] as const
32+
33+
type cases = [
34+
Expect<Equal<Length<typeof tesla>, 4>>,
35+
Expect<Equal<Length<typeof spaceX>, 5>>,
36+
// @ts-expect-error
37+
Length<5>,
38+
// @ts-expect-error
39+
Length<'hello world'>,
40+
]
41+
42+
/* _____________ 다음 단계 _____________ */
43+
/*
44+
> 정답 공유하기: https://tsch.js.org/18/answer/ko
45+
> 정답 보기: https://tsch.js.org/18/solutions
46+
> 다른 문제들: https://tsch.js.org/ko
47+
*/

백윤서/practice/268.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
268 - If
3+
-------
4+
by Pavel Glushkov (@pashutk) #쉬움 #utils
5+
6+
### 질문
7+
8+
조건 `C`, 참일 때 반환하는 타입 `T`, 거짓일 때 반환하는 타입 `F`를 받는 타입 `If`를 구현하세요. `C`는 `true` 또는 `false`이고, `T`와 `F`는 아무 타입입니다.
9+
10+
예시:
11+
12+
```ts
13+
type A = If<true, 'a', 'b'> // expected to be 'a'
14+
type B = If<false, 'a', 'b'> // expected to be 'b'
15+
```
16+
17+
> GitHub에서 보기: https://tsch.js.org/268/ko
18+
*/
19+
20+
/* _____________ 여기에 코드 입력 _____________ */
21+
22+
type If<C extends boolean, T, F> = C extends true ? T : F
23+
24+
/* _____________ 테스트 케이스 _____________ */
25+
import type { Equal, Expect } from '@type-challenges/utils'
26+
27+
type cases = [
28+
Expect<Equal<If<true, 'a', 'b'>, 'a'>>,
29+
Expect<Equal<If<false, 'a', 2>, 2>>,
30+
]
31+
32+
// @ts-expect-error
33+
type error = If<null, 'a', 'b'>
34+
35+
/* _____________ 다음 단계 _____________ */
36+
/*
37+
> 정답 공유하기: https://tsch.js.org/268/answer/ko
38+
> 정답 보기: https://tsch.js.org/268/solutions
39+
> 다른 문제들: https://tsch.js.org/ko
40+
*/

백윤서/practice/3057.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
3057 - Push
3+
-------
4+
by jiangshan (@jiangshanmeta) #쉬움 #array
5+
6+
### 질문
7+
8+
`Array.push`의 제네릭 버전을 구현하세요.
9+
10+
예시:
11+
12+
```typescript
13+
type Result = Push<[1, 2], '3'> // [1, 2, '3']
14+
```
15+
16+
> GitHub에서 보기: https://tsch.js.org/3057/ko
17+
*/
18+
19+
/* _____________ 여기에 코드 입력 _____________ */
20+
21+
type Push<T extends unknown[], U> = [...T, U]
22+
23+
/* _____________ 테스트 케이스 _____________ */
24+
import type { Equal, Expect } from '@type-challenges/utils'
25+
26+
type cases = [
27+
Expect<Equal<Push<[], 1>, [1]>>,
28+
Expect<Equal<Push<[1, 2], '3'>, [1, 2, '3']>>,
29+
Expect<Equal<Push<['1', 2, '3'], boolean>, ['1', 2, '3', boolean]>>,
30+
]
31+
32+
/* _____________ 다음 단계 _____________ */
33+
/*
34+
> 정답 공유하기: https://tsch.js.org/3057/answer/ko
35+
> 정답 보기: https://tsch.js.org/3057/solutions
36+
> 다른 문제들: https://tsch.js.org/ko
37+
*/

백윤서/practice/3060.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
3060 - Unshift
3+
-------
4+
by jiangshan (@jiangshanmeta) #쉬움 #array
5+
6+
### 질문
7+
8+
`Array.unshift`의 타입 버전을 구현하세요.
9+
10+
예시:
11+
12+
```typescript
13+
type Result = Unshift<[1, 2], 0> // [0, 1, 2,]
14+
```
15+
16+
> GitHub에서 보기: https://tsch.js.org/3060/ko
17+
*/
18+
19+
/* _____________ 여기에 코드 입력 _____________ */
20+
21+
type Unshift<T extends unknown[], U> = [U, ...T]
22+
23+
/* _____________ 테스트 케이스 _____________ */
24+
import type { Equal, Expect } from '@type-challenges/utils'
25+
26+
type cases = [
27+
Expect<Equal<Unshift<[], 1>, [1]>>,
28+
Expect<Equal<Unshift<[1, 2], 0>, [0, 1, 2]>>,
29+
Expect<Equal<Unshift<['1', 2, '3'], boolean>, [boolean, '1', 2, '3']>>,
30+
]
31+
32+
/* _____________ 다음 단계 _____________ */
33+
/*
34+
> 정답 공유하기: https://tsch.js.org/3060/answer/ko
35+
> 정답 보기: https://tsch.js.org/3060/solutions
36+
> 다른 문제들: https://tsch.js.org/ko
37+
*/

백윤서/practice/3312.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
3312 - Parameters
3+
-------
4+
by midorizemi (@midorizemi) #쉬움 #infer #tuple #built-in
5+
6+
### 질문
7+
8+
내장 제네릭 `Parameters<T>`를 이를 사용하지 않고 구현하세요.
9+
10+
예시:
11+
12+
```ts
13+
const foo = (arg1: string, arg2: number): void => {}
14+
15+
type FunctionParamsType = MyParameters<typeof foo> // [arg1: string, arg2: number]
16+
```
17+
18+
> GitHub에서 보기: https://tsch.js.org/3312/ko
19+
*/
20+
21+
/* _____________ 여기에 코드 입력 _____________ */
22+
23+
type MyParameters<T extends (...args: any[]) => any> = T extends (...args: infer R) => unknown ? R : unknown;
24+
25+
/* _____________ 테스트 케이스 _____________ */
26+
import type { Equal, Expect } from '@type-challenges/utils'
27+
28+
const foo = (arg1: string, arg2: number): void => { }
29+
const bar = (arg1: boolean, arg2: { a: 'A' }): void => { }
30+
const baz = (): void => { }
31+
32+
type cases = [
33+
Expect<Equal<MyParameters<typeof foo>, [string, number]>>,
34+
Expect<Equal<MyParameters<typeof bar>, [boolean, { a: 'A' }]>>,
35+
Expect<Equal<MyParameters<typeof baz>, []>>,
36+
]
37+
38+
/* _____________ 다음 단계 _____________ */
39+
/*
40+
> 정답 공유하기: https://tsch.js.org/3312/answer/ko
41+
> 정답 보기: https://tsch.js.org/3312/solutions
42+
> 다른 문제들: https://tsch.js.org/ko
43+
*/

0 commit comments

Comments
 (0)