Skip to content

Commit 5d81afc

Browse files
committed
add some medium ts challenges
1 parent 164ae27 commit 5d81afc

File tree

8 files changed

+416
-0
lines changed

8 files changed

+416
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
2 - Get Return Type
3+
-------
4+
by Anthony Fu (@antfu) #medium #infer #built-in
5+
6+
### Question
7+
8+
Implement the built-in `ReturnType<T>` generic without using it.
9+
10+
For example
11+
12+
```ts
13+
const fn = (v: boolean) => {
14+
if (v)
15+
return 1
16+
else
17+
return 2
18+
}
19+
20+
type a = MyReturnType<typeof fn> // should be "1 | 2"
21+
```
22+
23+
> View on GitHub: https://tsch.js.org/2
24+
*/
25+
26+
/* _____________ Your Code Here _____________ */
27+
28+
type MyReturnType<T extends (...args: any[]) => any> = T extends (
29+
...args: any[]
30+
) => infer R
31+
? R
32+
: never;
33+
34+
/* _____________ Test Cases _____________ */
35+
import type { Equal, Expect } from "@type-challenges/utils";
36+
37+
type cases = [
38+
Expect<Equal<string, MyReturnType<() => string>>>,
39+
Expect<Equal<123, MyReturnType<() => 123>>>,
40+
Expect<Equal<ComplexObject, MyReturnType<() => ComplexObject>>>,
41+
Expect<Equal<Promise<boolean>, MyReturnType<() => Promise<boolean>>>>,
42+
Expect<Equal<() => "foo", MyReturnType<() => () => "foo">>>,
43+
Expect<Equal<1 | 2, MyReturnType<typeof fn>>>,
44+
Expect<Equal<1 | 2, MyReturnType<typeof fn1>>>
45+
];
46+
47+
type ComplexObject = {
48+
a: [12, "foo"];
49+
bar: "hello";
50+
prev(): number;
51+
};
52+
53+
const fn = (v: boolean) => (v ? 1 : 2);
54+
const fn1 = (v: boolean, w: any) => (v ? 1 : 2);
55+
56+
/* _____________ Further Steps _____________ */
57+
/*
58+
> Share your solutions: https://tsch.js.org/2/answer
59+
> View solutions: https://tsch.js.org/2/solutions
60+
> More Challenges: https://tsch.js.org
61+
*/
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
3 - Omit
3+
-------
4+
by Anthony Fu (@antfu) #medium #union #built-in
5+
6+
### Question
7+
8+
Implement the built-in `Omit<T, K>` generic without using it.
9+
10+
Constructs a type by picking all properties from `T` and then removing `K`
11+
12+
For example
13+
14+
```ts
15+
interface Todo {
16+
title: string
17+
description: string
18+
completed: boolean
19+
}
20+
21+
type TodoPreview = MyOmit<Todo, 'description' | 'title'>
22+
23+
const todo: TodoPreview = {
24+
completed: false,
25+
}
26+
```
27+
28+
> View on GitHub: https://tsch.js.org/3
29+
*/
30+
31+
/* _____________ Your Code Here _____________ */
32+
33+
type MyOmit<T , K extends keyof T> = {
34+
[P in keyof T as P extends K ? never : P] : T[P]
35+
}
36+
37+
type TodoPreview = MyOmit<Todo, 'description' | 'title'>
38+
39+
const todo: TodoPreview = {
40+
completed: false,
41+
}
42+
43+
/* _____________ Test Cases _____________ */
44+
import type { Equal, Expect } from '@type-challenges/utils'
45+
46+
type cases = [
47+
Expect<Equal<Expected1, MyOmit<Todo, 'description'>>>,
48+
Expect<Equal<Expected2, MyOmit<Todo, 'description' | 'completed'>>>,
49+
]
50+
51+
// @ts-expect-error
52+
type error = MyOmit<Todo, 'description' | 'invalid'>
53+
54+
interface Todo {
55+
title: string
56+
description: string
57+
completed: boolean
58+
}
59+
60+
interface Expected1 {
61+
title: string
62+
completed: boolean
63+
}
64+
65+
interface Expected2 {
66+
title: string
67+
}
68+
69+
/* _____________ Further Steps _____________ */
70+
/*
71+
> Share your solutions: https://tsch.js.org/3/answer
72+
> View solutions: https://tsch.js.org/3/solutions
73+
> More Challenges: https://tsch.js.org
74+
*/
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) #easy #utils
5+
6+
### Question
7+
8+
Implement the util type `If<C, T, F>` which accepts condition `C`, a truthy value `T`, and a falsy value `F`. `C` is expected to be either `true` or `false` while `T` and `F` can be any type.
9+
10+
For example:
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+
> View on GitHub: https://tsch.js.org/268
18+
*/
19+
20+
/* _____________ Your Code Here _____________ */
21+
22+
type If<C extends boolean, T, F> = C extends true ? T : F
23+
24+
/* _____________ Test Cases _____________ */
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+
/* _____________ Further Steps _____________ */
36+
/*
37+
> Share your solutions: https://tsch.js.org/268/answer
38+
> View solutions: https://tsch.js.org/268/solutions
39+
> More Challenges: https://tsch.js.org
40+
*/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
533 - Concat
3+
-------
4+
by Andrey Krasovsky (@bre30kra69cs) #easy #array
5+
6+
### Question
7+
8+
Implement the JavaScript `Array.concat` function in the type system. A type takes the two arguments. The output should be a new array that includes inputs in ltr order
9+
10+
For example:
11+
12+
```ts
13+
type Result = Concat<[1], [2]> // expected to be [1, 2]
14+
```
15+
16+
> View on GitHub: https://tsch.js.org/533
17+
*/
18+
19+
/* _____________ Your Code Here _____________ */
20+
21+
type Tuple = readonly unknown[];
22+
23+
type Concat<T extends Tuple, U extends Tuple> = [...T, ...U]
24+
type Result2 = Concat<[], []>; // expected to be [1, 2]
25+
type Result3 = Concat<[], [1]>; // expected to be [1]
26+
type Result = Concat<[1], [2]>; // expected to be [1, 2]
27+
28+
/* _____________ Test Cases _____________ */
29+
import type { Equal, Expect } from "@type-challenges/utils";
30+
31+
const tuple = [1] as const;
32+
33+
type cases = [
34+
Expect<Equal<Concat<[], []>, []>>,
35+
Expect<Equal<Concat<[], [1]>, [1]>>,
36+
Expect<Equal<Concat<[1], [2]>, [1, 2]>>,
37+
Expect<Equal<Concat<typeof tuple, typeof tuple>, [1, 1]>>,
38+
Expect<Equal<Concat<[1, 2], [3, 4]>, [1, 2, 3, 4]>>,
39+
Expect<
40+
Equal<
41+
Concat<["1", 2, "3"], [false, boolean, "4"]>,
42+
["1", 2, "3", false, boolean, "4"]
43+
>
44+
>
45+
];
46+
47+
// @ts-expect-error
48+
type error = Concat<null, undefined>;
49+
50+
/* _____________ Further Steps _____________ */
51+
/*
52+
> Share your solutions: https://tsch.js.org/533/answer
53+
> View solutions: https://tsch.js.org/533/solutions
54+
> More Challenges: https://tsch.js.org
55+
*/
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
898 - Includes
3+
-------
4+
by null (@kynefuk) #easy #array
5+
6+
### Question
7+
8+
Implement the JavaScript `Array.includes` function in the type system. A type takes the two arguments. The output should be a boolean `true` or `false`.
9+
10+
For example:
11+
12+
```ts
13+
type isPillarMen = Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'> // expected to be `false`
14+
```
15+
16+
> View on GitHub: https://tsch.js.org/898
17+
*/
18+
19+
/* _____________ Your Code Here _____________ */
20+
21+
type Includes<T extends readonly any[], U> = {
22+
[P in T[number]] : true
23+
}[U] extends true ? true : false
24+
25+
26+
type isPillarMen = Includes<["Kars", "Esidisi", "Wamuu", "Santana"], "Dio">; // expected to be `false`
27+
type isPillarMen2 = Includes<["Kars", "Esidisi", "Wamuu", "Santana"], "Kars">; // expected to be `true`
28+
type isPillarMen3 = Includes<[{}], { a: "A" }>; // expected to be `false`
29+
30+
/* _____________ Test Cases _____________ */
31+
import type { Equal, Expect } from "@type-challenges/utils";
32+
33+
type cases = [
34+
Expect<Equal<Includes<[{}], { a: "A" }>, false>>,
35+
Expect<
36+
Equal<Includes<["Kars", "Esidisi", "Wamuu", "Santana"], "Kars">, true>
37+
>,
38+
Expect<
39+
Equal<Includes<["Kars", "Esidisi", "Wamuu", "Santana"], "Dio">, false>
40+
>,
41+
42+
Expect<Equal<Includes<[1, 2, 3, 5, 6, 7], 7>, true>>,
43+
Expect<Equal<Includes<[1, 2, 3, 5, 6, 7], 4>, false>>,
44+
Expect<Equal<Includes<[1, 2, 3], 2>, true>>,
45+
Expect<Equal<Includes<[1, 2, 3], 1>, true>>,
46+
Expect<Equal<Includes<[boolean, 2, 3, 5, 6, 7], false>, false>>,
47+
Expect<Equal<Includes<[true, 2, 3, 5, 6, 7], boolean>, false>>,
48+
Expect<Equal<Includes<[false, 2, 3, 5, 6, 7], false>, true>>,
49+
Expect<Equal<Includes<[{ a: "A" }], { readonly a: "A" }>, false>>,
50+
Expect<Equal<Includes<[{ readonly a: "A" }], { a: "A" }>, false>>,
51+
Expect<Equal<Includes<[1], 1 | 2>, false>>,
52+
Expect<Equal<Includes<[1 | 2], 1>, false>>,
53+
Expect<Equal<Includes<[null], undefined>, false>>,
54+
Expect<Equal<Includes<[undefined], null>, false>>
55+
];
56+
57+
/* _____________ Further Steps _____________ */
58+
/*
59+
> Share your solutions: https://tsch.js.org/898/answer
60+
> View solutions: https://tsch.js.org/898/solutions
61+
> More Challenges: https://tsch.js.org
62+
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
3057 - Push
3+
-------
4+
by jiangshan (@jiangshanmeta) #easy #array
5+
6+
### Question
7+
8+
Implement the generic version of ```Array.push```
9+
10+
For example:
11+
12+
```typescript
13+
type Result = Push<[1, 2], '3'> // [1, 2, '3']
14+
```
15+
16+
> View on GitHub: https://tsch.js.org/3057
17+
*/
18+
19+
/* _____________ Your Code Here _____________ */
20+
21+
type Push<T extends any[], U> = U extends any[]
22+
? [...T, ...U]
23+
: U extends boolean
24+
? [...T, boolean]
25+
: [...T, U];
26+
27+
type Result = Push<[1, 2], boolean>; // [1, 2, boolean]
28+
29+
/* _____________ Test Cases _____________ */
30+
import type { Equal, Expect } from "@type-challenges/utils";
31+
32+
type cases = [
33+
Expect<Equal<Push<[], 1>, [1]>>,
34+
Expect<Equal<Push<[1,2,3], [4,5 ,boolean]>, [1, 2, 3, 4, 5, boolean,]>>,
35+
Expect<Equal<Push<[1, 2], "3">, [1, 2, "3"]>>,
36+
Expect<Equal<Push<["1", 2, "3"], boolean>, ["1", 2, "3", boolean]>>
37+
];
38+
39+
/* _____________ Further Steps _____________ */
40+
/*
41+
> Share your solutions: https://tsch.js.org/3057/answer
42+
> View solutions: https://tsch.js.org/3057/solutions
43+
> More Challenges: https://tsch.js.org
44+
*/
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) #easy #array
5+
6+
### Question
7+
8+
Implement the type version of ```Array.unshift```
9+
10+
For example:
11+
12+
```typescript
13+
type Result = Unshift<[1, 2], 0> // [0, 1, 2,]
14+
```
15+
16+
> View on GitHub: https://tsch.js.org/3060
17+
*/
18+
19+
/* _____________ Your Code Here _____________ */
20+
21+
type Unshift<T extends any[], U> = [U, ...T]
22+
23+
/* _____________ Test Cases _____________ */
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+
/* _____________ Further Steps _____________ */
33+
/*
34+
> Share your solutions: https://tsch.js.org/3060/answer
35+
> View solutions: https://tsch.js.org/3060/solutions
36+
> More Challenges: https://tsch.js.org
37+
*/

0 commit comments

Comments
 (0)