You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 01-the-data/the-data.md
+17-17Lines changed: 17 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
1
# The data of type-level programs
2
2
3
-
Like with every programming language, in Type-Level TypeScript we will write programs that **transform some data** but unlike in other programming languages, the data our programs will transform are **types**!
3
+
Like with every programming language, in TypeLevel Typescript we will write code that **transform some data**. The only thing that's different from most other languages is that the data our code will transform are **types**! We will write programs that take some types as input and output some other types.
4
4
5
-
We will write code that take some types as input and output other types. To master this language we will need to start by understanding the different sorts of data we have at our disposal.
5
+
To master this language we will need to start by understanding the different sorts of data we have at our disposal.
6
6
7
7
## The three families of types
8
8
9
9
There are 3 main "families" of data in Type-Level TypeScript (TLTS): primitive types, literal types, and data structure types. Let's explore each of them.
10
10
11
11
### Primitive types
12
12
13
-
You are certainly already familiar with primitive types. We use them a lot to annotate our function arguments and return types in our day to day TypeScript code. Here is the list of primitive types:
13
+
You are certainly already familiar with primitive types. We use them a lot to annotate our variables and functions in our day to day TypeScript code. Here is the list of primitive types:
14
14
15
15
```ts
16
16
typePrimitives=
@@ -27,7 +27,7 @@ Every JavaScript value at the exception of objects and functions belong to one o
27
27
28
28
### Literal types
29
29
30
-
Literal types are "exact" types, which encompass a **single value**.
30
+
Literal types are "exact" types, which encompass a **single possible value**.
31
31
32
32
<!-- prettier-ignore -->
33
33
```ts
@@ -50,39 +50,39 @@ There's an infinite number of literal types and they look just like regular valu
50
50
51
51
The world of values and the world of types mostly belong to two parallel universes which exist separately but can't "touch" each other and can't be mixed together [(1)](#dependent-types). I think it's helpful to consider literal types as a sort of reflection of values in the world of types, but we need to keep in mind that they are different things.
52
52
53
-
One notable difference between values and literal types is that you can't write arithmetic expressions at the type-level. For instance `type five = 2 + 3` won't work, even if `const five = 2 + 3` is a perfectly valid value-level expression.
53
+
One notable difference between values and literal types is that you can't write arithmetic expressions at the type-level. For instance `type five = 2 + 3` won't work, even if `const five = 2 + 3` is a perfectly valid piece of value-level code.
54
54
55
-
Literal types become particarly useful when put in unions to describe variables which can only contain a finite set of possible values, like `let trafficLight: "green" | "orange" | "red";`. We will explore union types in more depths in a bit.
55
+
Literal types become particarly useful when put in unions to describe variables which can only contain a finite set of possible values like `let trafficLight: "green" | "orange" | "red";`. We will explore union types in more depths in a bit.
56
56
57
57
### Data structures
58
58
59
59
In our type-level world, we have four main built-in data structures at our disposal: objects, records, tuples and arrays.
60
60
61
61
```ts
62
62
typeDataStructures=
63
-
| { key1:1; key2:2 } // objects
63
+
| { key1:boolean; key2:number } // objects
64
64
| { [key:string]:number } // records
65
-
| [1, 2] // tuples
65
+
| [boolean, number] // tuples
66
66
|number[]; // arrays
67
67
```
68
68
69
-
-**Object types** describe objects have a finite number of keys, and each of them contain a value of some specific type.
70
-
-**Record types** are like object types, except they describe objects with an unknown number of keys, and every value contained in a record must have the same type. The type `{ [key: string]: number }`tells us that objects of this type contain numbers.
71
-
-**Tuple types** describe arrays with a fixed length. They can contain a value of a different type for each index.
72
-
-**Array types** describe arrays with an unknown length. Every value contained in an array must have the same type.
69
+
-**Object types** describe objects with a finite set of keys, and these keys contain values of potentially different types.
70
+
-**Record types** are similar to object types, except they describe objects with an unknown number of keys, and all values contained in a record share the same type. For example all values in an object of type `{ [key: string]: number }`are numbers.
71
+
-**Tuple types** describe arrays with a fixed length. They can have a different type for each index.
72
+
-**Array types** describe arrays with an unknown length. all values contained in an array type must have the same type.
73
73
74
74
It makes sense to split those in 2 sub-groups:
75
75
76
76
- The sub-group of data structures that can contain **several types**: objects and tuples.
77
77
- The sub-group of data structures that contain a **single type**: records and arrays.
78
78
79
-
At the type-level, we will mostly be using Objects and Tuples. They are the type level equivalent of JavaScript's objects and arrays and they will enable to implement some of the mapping and filtering algorithms you are already familiar with.
79
+
At the type-level, we will mostly be using objects and tuples. They are the type level equivalent of JavaScript's objects and arrays and they will enable us to implement some of the algorithms you are already familiar with.
80
80
81
-
Since Records and Array types can only contain a single type inside of them, they aren't very useful in the world of types, so we won't use them very much.
81
+
Since records and array types can only contain a single type inside of them, they aren't very useful in type-level programs, so we won't use them very much.
82
82
83
83
### Union and Intersections are data structures too!
84
84
85
-
Everything we have seen so far look somewhat similar to the kind of data we have at the value-level, but unions and intersections are different. They are really specific to the type-level, and building a good mental model of how they work is a little more challenging.
85
+
Everything we have seen so far looks somewhat similar to the kind of data we have at the value-level, but unions and intersections are different. They are really specific to the type-level, and building a good mental model of how they work is a little more challenging.
86
86
87
87
Here is what they look like:
88
88
@@ -92,9 +92,9 @@ type Intersections = X & Y;
92
92
typeUnions=X|Y;
93
93
```
94
94
95
-
We often tend to think of `&` and `|` as operators. They definitely look like operators at first sight because we place them in between two other types, but they actually are data structures.
95
+
We often tend to think of `&` and `|` as operators. They definitely look like operators at first sight because we place them in between two other types, but they actually are data structures too.
96
96
97
-
In this course, I'll show you how we can loop through each item inside a union type, how to transform each of them or filter some items to create a new union type from an existing one. This clearly means that `X | Y` doesn't destroy`X` and `Y`to create a new opaque type as an operator would. We will still be able to access `X` and `Y` afterwards and`|` is just a way to add them to some kind of "union" data structure.
97
+
In this course, we will see how we can loop through each item inside a union type, how to "map" over a union type to transform each of its items, or how to filter some items out to create a new union type from an existing one. This clearly means that `X | Y` doesn't turn`X` and `Y`into a new opaque type the way an operator would. Since we are still able to access `X` and `Y` afterwards, it looks like`|` is just a way to add them to some kind of "union" data structure.
98
98
99
99
To get a better grasp at how unions and intersections work, I'll need to take a little detour that is very foundamental to Type-level TypeScript: **all types are Sets**.
0 commit comments