Skip to content

Commit 96d26b3

Browse files
authored
[style]: run prettier on Markdown files (#145)
1 parent b760ccd commit 96d26b3

File tree

185 files changed

+1827
-1319
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+1827
-1319
lines changed

en/easy-concat.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The output should be a new array that includes inputs in ltr order.
1515
For example:
1616

1717
```ts
18-
type Result = Concat<[1], [2]> // expected to be [1, 2]
18+
type Result = Concat<[1], [2]>; // expected to be [1, 2]
1919
```
2020

2121
## Solution
@@ -40,14 +40,14 @@ Variadic Tuple Types allows us to model the same behavior in the type system.
4040
If we want to concatenate two generic arrays, we can return the new array where both arrays are behind the spread operator:
4141

4242
```ts
43-
type Concat<T, U> = [...T, ...U]
43+
type Concat<T, U> = [...T, ...U];
4444
```
4545

4646
We are getting the error “A rest element type must be an array type.”, though.
4747
Let us fix that by letting compiler know those types are arrays:
4848

4949
```ts
50-
type Concat<T extends unknown[], U extends unknown[]> = [...T, ...U]
50+
type Concat<T extends unknown[], U extends unknown[]> = [...T, ...U];
5151
```
5252

5353
## References

en/easy-first.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ Implement a generic `First<T>` that takes an Array `T` and returns it's first el
1313
For example:
1414

1515
```ts
16-
type arr1 = ['a', 'b', 'c']
17-
type arr2 = [3, 2, 1]
16+
type arr1 = ["a", "b", "c"];
17+
type arr2 = [3, 2, 1];
1818

19-
type head1 = First<arr1> // expected to be 'a'
20-
type head2 = First<arr2> // expected to be 3
19+
type head1 = First<arr1>; // expected to be 'a'
20+
type head2 = First<arr2>; // expected to be 3
2121
```
2222

2323
## Solution
2424

2525
The first thing that could come up is to use lookup types and just write `T[0]`:
2626

2727
```ts
28-
type First<T extends any[]> = T[0]
28+
type First<T extends any[]> = T[0];
2929
```
3030

3131
But there is an edge case that we need to handle.

en/easy-if.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Implement a utils `If` which accepts condition `C`, a truthy return type `T`, an
1414
For example:
1515

1616
```ts
17-
type A = If<true, 'a', 'b'> // expected to be 'a'
18-
type B = If<false, 'a', 'b'> // expected to be 'b'
17+
type A = If<true, "a", "b">; // expected to be 'a'
18+
type B = If<false, "a", "b">; // expected to be 'b'
1919
```
2020

2121
## Solution

en/easy-includes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ For example:
1515

1616
```typescript
1717
// expected to be `false`
18-
type isPillarMen = Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'>
18+
type isPillarMen = Includes<["Kars", "Esidisi", "Wamuu", "Santana"], "Dio">;
1919
```
2020

2121
## Solution

en/easy-pick.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ For example:
1616

1717
```ts
1818
interface Todo {
19-
title: string
20-
description: string
21-
completed: boolean
19+
title: string;
20+
description: string;
21+
completed: boolean;
2222
}
2323

24-
type TodoPreview = MyPick<Todo, 'title' | 'completed'>
24+
type TodoPreview = MyPick<Todo, "title" | "completed">;
2525

2626
const todo: TodoPreview = {
27-
title: 'Clean room',
27+
title: "Clean room",
2828
completed: false,
29-
}
29+
};
3030
```
3131

3232
## Solution
@@ -50,7 +50,7 @@ The type of values itself are going to be without change.
5050
Although, we need to take its type from the original type and that is where lookup type is useful:
5151

5252
```ts
53-
type MyPick<T, K extends keyof T> = { [P in K]: T[P] }
53+
type MyPick<T, K extends keyof T> = { [P in K]: T[P] };
5454
```
5555

5656
We are saying “get everything from `K`, name it as `P` and make it as a new key in our new object with a value type taken from the input type”.

en/easy-push.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Implement the generic version of `Array.push`.
1212
For example:
1313

1414
```typescript
15-
type Result = Push<[1, 2], '3'> // [1, 2, '3']
15+
type Result = Push<[1, 2], "3">; // [1, 2, '3']
1616
```
1717

1818
## Solution
@@ -25,22 +25,22 @@ To get all the elements from an array, we can use variadic tuple types.
2525
So, let us return an array with the same elements from input type `T`:
2626

2727
```typescript
28-
type Push<T, U> = [...T]
28+
type Push<T, U> = [...T];
2929
```
3030

3131
Getting a compilation error “A rest element type must be an array type”.
3232
It means that we can not use variadic tuple type on non-array-like types.
3333
So let us add a generic constraint to show that we are working with arrays only:
3434

3535
```typescript
36-
type Push<T extends unknown[], U> = [...T]
36+
type Push<T extends unknown[], U> = [...T];
3737
```
3838

3939
Now, we have a copy of the incoming array in type parameter `T`.
4040
The only thing remains is to add an element from `U`:
4141

4242
```typescript
43-
type Push<T extends unknown[], U> = [...T, U]
43+
type Push<T extends unknown[], U> = [...T, U];
4444
```
4545

4646
That way, we have implemented a push operation in the type system.

en/easy-readonly.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ For example:
1616

1717
```ts
1818
interface Todo {
19-
title: string
20-
description: string
19+
title: string;
20+
description: string;
2121
}
2222

2323
const todo: MyReadonly<Todo> = {
2424
title: "Hey",
25-
description: "foobar"
26-
}
25+
description: "foobar",
26+
};
2727

28-
todo.title = "Hello" // Error: cannot reassign a readonly property
29-
todo.description = "barFoo" // Error: cannot reassign a readonly property
28+
todo.title = "Hello"; // Error: cannot reassign a readonly property
29+
todo.description = "barFoo"; // Error: cannot reassign a readonly property
3030
```
3131

3232
## Solution
@@ -38,7 +38,7 @@ We are going to use the usual [Mapped Type](https://www.typescriptlang.org/docs/
3838
For each property in the type, we take its key and add a `readonly` modifier to it:
3939

4040
```ts
41-
type MyReadonly<T> = { readonly [K in keyof T]: T[K] }
41+
type MyReadonly<T> = { readonly [K in keyof T]: T[K] };
4242
```
4343

4444
## References

en/easy-tuple-length.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@ For given a tuple, you need create a generic `Length`, pick the length of the tu
1313
For example:
1414

1515
```ts
16-
type tesla = ['tesla', 'model 3', 'model X', 'model Y']
17-
type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT']
18-
19-
type teslaLength = Length<tesla> // expected 4
20-
type spaceXLength = Length<spaceX> // expected 5
16+
type tesla = ["tesla", "model 3", "model X", "model Y"];
17+
type spaceX = [
18+
"FALCON 9",
19+
"FALCON HEAVY",
20+
"DRAGON",
21+
"STARSHIP",
22+
"HUMAN SPACEFLIGHT"
23+
];
24+
25+
type teslaLength = Length<tesla>; // expected 4
26+
type spaceXLength = Length<spaceX>; // expected 5
2127
```
2228

2329
## Solution
@@ -26,14 +32,14 @@ We know that we can use property `length` to access the length of the array in J
2632
We can do the same in types as well:
2733

2834
```ts
29-
type Length<T extends any> = T['length']
35+
type Length<T extends any> = T["length"];
3036
```
3137

3238
But going that way we will get the compilation error “Type 'length' cannot be used to index type 'T'.”.
3339
So we need to give a hint to TypeScript and tell that our input type parameter has this property:
3440

3541
```ts
36-
type Length<T extends { length: number }> = T['length']
42+
type Length<T extends { length: number }> = T["length"];
3743
```
3844

3945
## References

en/easy-tuple-to-object.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ Given an array, transform to an object type and the key/value must in the given
1313
For example:
1414

1515
```ts
16-
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const
16+
const tuple = ["tesla", "model 3", "model X", "model Y"] as const;
1717

1818
// expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}
19-
const result: TupleToObject<typeof tuple>
19+
const result: TupleToObject<typeof tuple>;
2020
```
2121

2222
## Solution
@@ -28,7 +28,7 @@ We can get the values from an array by using `T[number]` construct.
2828
With the help of mapped types, we can iterate over those values in `T[number]` and return a new type where the key and value is the type from `T[number]`:
2929

3030
```ts
31-
type TupleToObject<T extends readonly any[]> = { [K in T[number]]: K }
31+
type TupleToObject<T extends readonly any[]> = { [K in T[number]]: K };
3232
```
3333

3434
## References

en/easy-unshift.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Implement the type version of `Array.unshift()`.
1212
For example:
1313

1414
```typescript
15-
type Result = Unshift<[1, 2], 0> // [0, 1, 2]
15+
type Result = Unshift<[1, 2], 0>; // [0, 1, 2]
1616
```
1717

1818
## Solution
@@ -24,22 +24,22 @@ Here we do pretty the same, but in a different order.
2424
First, let us take all the elements from the incoming array:
2525

2626
```typescript
27-
type Unshift<T, U> = [...T]
27+
type Unshift<T, U> = [...T];
2828
```
2929

3030
With this snippet, we are getting the compilation error “A rest element type must be an array type”.
3131
Let us fix the error by adding a constraint over the type parameter:
3232

3333
```typescript
34-
type Unshift<T extends unknown[], U> = [...T]
34+
type Unshift<T extends unknown[], U> = [...T];
3535
```
3636

3737
Now, we have the same array as the incoming one.
3838
All we need is to add an element to the beginning of the tuple.
3939
Let’s do just that:
4040

4141
```typescript
42-
type Unshift<T extends unknown[], U> = [U, ...T]
42+
type Unshift<T extends unknown[], U> = [U, ...T];
4343
```
4444

4545
That way, we made an “unshift” function in the type system!

en/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Type Challenges Solutions
33
description: >-
44
This project is aimed at helping you better understand how the type system
55
works, writing your own utilities, or just having fun with the challenges.
6-
keywords: 'type, challenges, solutions, typescript, javascript'
6+
keywords: "type, challenges, solutions, typescript, javascript"
77
lang: en
88
comments: false
99
---

en/medium-absolute.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ Remove the “-” sign.
2828
We can approach it by checking if the type has a “-” sign in its template literal type, and if so, we infer the part without the “-” sign, otherwise return the type itself:
2929

3030
```typescript
31-
type Absolute<T extends number | string | bigint> = T extends `-${infer N}` ? N : T;
31+
type Absolute<T extends number | string | bigint> = T extends `-${infer N}`
32+
? N
33+
: T;
3234
```
3335

3436
So, e.g. if we provide the type `T = “-50”`, it will match the `“-<N>”`, where `N` will become just “50”.
@@ -41,7 +43,9 @@ When providing a positive number, it will not match the literal type and return
4143
Let us fix that by wrapping our `T` in template literal type:
4244

4345
```typescript
44-
type Absolute<T extends number | string | bigint> = T extends `-${infer N}` ? N : `${T}`;
46+
type Absolute<T extends number | string | bigint> = T extends `-${infer N}`
47+
? N
48+
: `${T}`;
4549
```
4650

4751
Still, some tests are failing.
@@ -50,7 +54,9 @@ Number will not match the template literal type of condition, so it will return
5054
To overcome this, we can convert the number to string:
5155

5256
```typescript
53-
type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer N}` ? N : `${T}`;
57+
type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer N}`
58+
? N
59+
: `${T}`;
5460
```
5561

5662
As a result, we got a type that takes any `number`, `string`, `bigint` and converts it to the string.

en/medium-anyof.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ I don’t like it, take a look:
3434

3535
```typescript
3636
type AnyOf<T extends readonly any[], I = T[number]> = (
37-
I extends any ?
38-
I extends Falsy ?
39-
false :
40-
true :
41-
never
42-
) extends false ? false : true;
37+
I extends any ? (I extends Falsy ? false : true) : never
38+
) extends false
39+
? false
40+
: true;
4341
```
4442

4543
So I’ve started thinking, can we make it more maintainable?
@@ -60,16 +58,16 @@ First, we can construct a type that represents false-y types.
6058
Let’s call it `Falsy`:
6159

6260
```typescript
63-
type Falsy = 0 | '' | false | [] | { [P in any]: never }
61+
type Falsy = 0 | "" | false | [] | { [P in any]: never };
6462
```
6563

6664
Having a type that represents false-y values, we can just use a conditional type to check if the `H` extends from the type:
6765

6866
```typescript
6967
type AnyOf<T extends readonly any[]> = T extends [infer H, ...infer T]
7068
? H extends Falsy
71-
? never
72-
: never
69+
? never
70+
: never
7371
: never;
7472
```
7573

@@ -80,8 +78,8 @@ So we can continue recursively:
8078
```typescript
8179
type AnyOf<T extends readonly any[]> = T extends [infer H, ...infer T]
8280
? H extends Falsy
83-
? AnyOf<T>
84-
: never
81+
? AnyOf<T>
82+
: never
8583
: never;
8684
```
8785

@@ -92,8 +90,8 @@ So we just exit from recursion by returning `true` type literal:
9290
```typescript
9391
type AnyOf<T extends readonly any[]> = T extends [infer H, ...infer T]
9492
? H extends Falsy
95-
? AnyOf<T>
96-
: true
93+
? AnyOf<T>
94+
: true
9795
: never;
9896
```
9997

@@ -104,21 +102,21 @@ We can return `false` in such case:
104102
```typescript
105103
type AnyOf<T extends readonly any[]> = T extends [infer H, ...infer T]
106104
? H extends Falsy
107-
? AnyOf<T>
108-
: true
105+
? AnyOf<T>
106+
: true
109107
: false;
110108
```
111109

112110
That’s how we made an implementation of `AnyOf` function in the type system.
113111
For the reference here is the whole implementation:
114112

115113
```typescript
116-
type Falsy = 0 | '' | false | [] | { [P in any]: never }
114+
type Falsy = 0 | "" | false | [] | { [P in any]: never };
117115

118116
type AnyOf<T extends readonly any[]> = T extends [infer H, ...infer T]
119117
? H extends Falsy
120-
? AnyOf<T>
121-
: true
118+
? AnyOf<T>
119+
: true
122120
: false;
123121
```
124122

0 commit comments

Comments
 (0)