Skip to content

Commit 5177bce

Browse files
jdwilkin4Jeevankumar-smoT01
authored
feat(curriculum): expand and reorganize initial typescript lessons (freeCodeCamp#65928)
Co-authored-by: Jeevankumar S <110320697+Jeevankumar-s@users.noreply.github.com> Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>
1 parent 120c5aa commit 5177bce

12 files changed

Lines changed: 1639 additions & 114 deletions

File tree

client/i18n/locales/english/intro.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3639,6 +3639,12 @@
36393639
"In these lessons, you will learn what TypeScript is and how to use it."
36403640
]
36413641
},
3642+
"lecture-understanding-type-composition": {
3643+
"title": "Understanding Type Composition",
3644+
"intro": [
3645+
"In these lessons, you will learn how to work with union types, interfaces, void types and more."
3646+
]
3647+
},
36423648
"lecture-working-with-generics-and-type-narrowing": {
36433649
"title": "Working with Generics and Type Narrowing",
36443650
"intro": [
Lines changed: 76 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,226 +1,189 @@
11
---
22
id: 67d3018062fe6ba92b7d8299
3-
title: How Do the Different Types Work in TypeScript?
3+
title: How Do Primitive Types Work in TypeScript?
44
challengeType: 19
5-
dashedName: how-do-the-different-types-work-in-typescript
5+
dashedName: how-do-primitive-types-work-in-typescript
66
---
77

88
# --description--
99

10-
You've already seen one in the previous lesson: `string[]`, which represents an array of strings. But how does that actually work?
10+
In this lesson, you will learn how to work with basic primitive types and how type annotations work for variables.
1111

12-
For the primitive data types `string`, `null`, `undefined`, `number`, `boolean`, and `bigint`, TypeScript offers corresponding type keywords. In our example, we are using these types to annotate our variables:
12+
For the primitive data types `string`, `null`, `undefined`, `number`, `boolean`, and `bigint`, TypeScript offers corresponding type keywords.
1313

14-
```js
15-
let str: string = "Naomi";
16-
let num: number = 42;
17-
let bool: boolean = true;
18-
let nope: null = null;
19-
let nada: undefined = undefined;
20-
```
14+
For this lesson, we will focus on the commonly used primitives and not go into much detail for `bigint`. The `bigint` data type is a numeric data type in JavaScript to represent numbers larger than the standard integer type.
2115

22-
Now, we have explicitly declared `str` as `string` - which might seem confusing because we assigned it a string value already, but this annotation ensures that we cannot reassign a different value type (like a number) to that variable.
16+
Let's take a look at each of these primitive types starting with `string`. Here is how you can type a string in TypeScript:
2317

24-
But what about arrays and objects? Well, you've already seen the syntax for an array! You can define an array of specific type with two different syntaxes:
25-
26-
```js
27-
const arrOne: string[] = ["Naomi"];
28-
const arrTwo: Array<string> = ["Camperchan"];
18+
```ts
19+
let firstName: string = "Angie";
2920
```
3021

31-
Fundamentally, these two syntaxes are the same, and the choice between the two is generally a matter of preference.
22+
Unlike vanilla JavaScript, the variable `firstName` is expecting a `string` type. So if you try to reassign it with a value of a number, then you will get an error like this:
3223

33-
Objects get a bit more complicated. You can define the exact structure of an object:
34-
35-
```js
36-
const obj: { a: string, b: number } = { a: "Naomi", b: 42 };
24+
```ts
25+
let firstName: string = "Angie";
26+
firstName = 9 // Type 'number' is not assignable to type 'string'.
3727
```
3828

39-
This syntax means that property `a` must always be a string, property `b` must always be a number, and you cannot add or remove properties.
40-
41-
But maybe you don't want to restrict the properties? Maybe you want an object with any keys, but the values all need to be strings. There are two ways you can do this:
42-
43-
```js
44-
const objOne: Record<string, string> = {};
45-
const objTwo: { [key: string]: string } = {};
46-
```
47-
48-
Like the array types, these are fundamentally similar. With these object types, you must define both the type of the keys and the type of the values. Keys must always be strings, but you can define custom string types to further restrict those keys.
49-
50-
In addition to these types, TypeScript offers four other helpful types. The first is `any`, which indicates that a value can have any type. This is effectively the Konami Code of TypeScript - it tells the compiler to stop caring about the type of that variable and let you do whatever.
51-
52-
The second is `unknown`, which is generally preferred over `any`. The `unknown` type tells TypeScript that you do care about the type of the value, but you don't actually know what it is. If you then try to perform type-specific actions (like a subtraction operator, or the `slice()` method to perform a specific string operation), TypeScript will expect you to narrow down the type of that value first. You'll learn more about type narrowing in an upcoming lesson.
29+
The next type we will look at is the `number` type. Here is an example:
5330

54-
The third is `void`. This is a special type that you'll typically only use when defining functions. It's effectively the opposite of `any` - it represents the absence of any type at all. Functions which don't have a return value, such as `console.log()`, have a return type of `void`.
55-
56-
Finally, there is the `never` type. This is probably something you won't use often - it represents a type that will never exist. For example, passing a mock object to a function in your test suite might be a good use of `never` to indicate that such an object would never really be passed to the function.
57-
58-
Expanding on these types, you have access to the `type` keyword. This keyword is like `const`, but instead of declaring a variable you can declare a type:
59-
60-
```js
61-
type myString = string;
31+
```ts
32+
let age: number = 16;
6233
```
6334

64-
This might not seem terribly useful on its own, but when coupled with union types it becomes powerful. A union type allows you to combine two or more types into one. Here's an example:
65-
66-
```js
67-
type stringOrNumber = string | number;
68-
```
35+
You can assign integer and floats to the `number` type. But if you were to try and reassign a value that is a `bigint` type, then you will get an error like this:
6936

70-
Our `stringOrNumber` type matches values that are a string and values that are a number. You can then combine your type with other types, like an array:
37+
```ts
38+
let age: number = 16;
7139

72-
```js
73-
const stuff: stringOrNumber[] = ["a", 2, "c", 1000];
40+
// Type 'bigint' is not assignable to type 'number'.
41+
age = 123n;
7442
```
7543

76-
You can also define more strict types that include only specific values:
44+
The next type we will look at is the `boolean` type. Here are some examples:
7745

78-
```js
79-
type bot = "camperchan" | "camperbot" | "naomi";
80-
type digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
46+
```ts
47+
let isLoggedIn: boolean = true;
48+
let isAdmin: boolean = false;
8149
```
8250

83-
You could then combine those types to create more specific restrictions on an object:
51+
The last two types are the `null` and `undefined` types.
8452

85-
```js
86-
const artificialIntelligence: Record<bot, digit> = { camperchan: 5 }
87-
```
53+
As you recall from earlier JavaScript lessons, `null` represents an intentional absence of a value. `undefined` means a variable has been declared but has not been assigned a value.
8854

89-
Though if you need more control over the structure of an object, chances are you'll reach for our final type: `interface`. Interfaces are effectively classes, but for types. They can implement or extend other interfaces, are specifically object types, and are generally preferred unless you need a specific feature offered by a type declaration.
55+
You will see examples of how to work with these types later on when you learn about function types and union types.
9056

91-
```js
92-
interface wowie {
93-
zowie: boolean;
94-
method: () => void;
95-
}
96-
```
57+
Now that you have seen how type annotations work with variables, the question arises on if you need to always explicitly type out your variables.
9758

98-
Finally, functions can also be given type signatures. In the previous lesson, you saw how to define the type of a particular parameter:
59+
If you have a simple type annotation like this, then you might not need to use an explicit type.
9960

100-
```js
101-
const getRandomValue = (array: string[]) => {
102-
return array[Math.floor(Math.random() * array.length)];
103-
}
61+
```ts
62+
let firstName: string = "Angie";
10463
```
10564

106-
But you can also define the return type of the function.
65+
Reason being is because TypeScript will try to automatically infer the types based on its value. So if you write this, TypeScript will understand it is a string.
10766

10867
```js
109-
const getRandomValue = (array: string[]): string => {
110-
return array[Math.floor(Math.random() * array.length)];
111-
}
68+
let firstName = "Angie";
11269
```
11370

114-
In this example, we've told TypeScript that the function should return a string. If we try to return anything else, TypeScript will provide a compiler error to let us know.
115-
116-
And that covers the basics of TypeScript's type system. It's pretty complex, and has a lot of moving parts, but it can often help to think of it as mirroring JavaScript's types.
71+
You should explicitly type your variables when TypeScript cannot clearly infer the type or when you want to enforce a specific type for situations like function parameters or complex objects.
11772

11873
# --questions--
11974

12075
## --text--
12176

122-
Which of the following is NOT a primitive data type in TypeScript?
77+
Which of the following is the correct way to explicitly add a type annotation to a number variable?
12378

12479
## --answers--
12580

126-
`string`
127-
128-
### --feedback--
129-
130-
Primitive data types are the most basic data types available in TypeScript.
81+
```ts
82+
let age: number = 16;
83+
```
13184

13285
---
13386

134-
`number`
87+
```ts
88+
let age<> number = 16;
89+
```
13590

13691
### --feedback--
13792

138-
Primitive data types are the most basic data types available in TypeScript.
93+
Refer back to the beginning of the lesson for number types.
13994

14095
---
14196

142-
`boolean`
97+
```ts
98+
let age>> number = 16;
99+
```
143100

144101
### --feedback--
145102

146-
Primitive data types are the most basic data types available in TypeScript.
103+
Refer back to the beginning of the lesson for number types.
147104

148105
---
149106

150-
`array`
107+
```ts
108+
let age == number = 16;
109+
```
110+
111+
### --feedback--
112+
113+
Refer back to the beginning of the lesson for number types.
151114

152115
## --video-solution--
153116

154-
4
117+
1
155118

156119
## --text--
157120

158-
What is the purpose of the `unknown` type in TypeScript?
121+
Which of the following is NOT a primitive data type?
159122

160123
## --answers--
161124

162-
It allows any type of value to be assigned without type checking.
125+
`string`
163126

164127
### --feedback--
165128

166-
Unlike `any`, `unknown` requires type narrowing before specific operations can be performed.
129+
Refer back to the beginning of the lesson.
167130

168131
---
169132

170-
It indicates that you don't know the type and need to narrow it before use.
133+
`boolean`
171134

172-
---
135+
### --feedback--
173136

174-
It represents the absence of any type at all.
137+
Refer back to the beginning of the lesson.
175138

176-
### --feedback--
139+
---
177140

178-
Unlike `any`, `unknown` requires type narrowing before specific operations can be performed.
141+
`object`
179142

180143
---
181144

182-
It represents a type that will never exist.
145+
`number`
183146

184147
### --feedback--
185148

186-
Unlike `any`, `unknown` requires type narrowing before specific operations can be performed.
149+
Refer back to the beginning of the lesson.
187150

188151
## --video-solution--
189152

190-
2
153+
3
191154

192155
## --text--
193156

194-
Which keyword is used to declare a custom type in TypeScript?
157+
When should you explicitly type your variables in TypeScript?
195158

196159
## --answers--
197160

198-
`interface`
161+
When you are working with `number` and `bigint` types.
199162

200163
### --feedback--
201164

202-
This keyword is used to define a contract for object shapes, not to declare custom types.
165+
Refer back to the end of the lesson.
203166

204167
---
205168

206-
`type`
169+
When you are working with `null` and `undefined` types.
207170

208-
---
171+
### --feedback--
209172

210-
`custom`
173+
Refer back to the end of the lesson.
211174

212-
### --feedback--
175+
---
213176

214-
This is not a valid keyword in TypeScript.
177+
When TypeScript cannot clearly infer the type.
215178

216179
---
217180

218-
`define`
181+
You should always have explicit types in your code.
219182

220183
### --feedback--
221184

222-
This is not a valid keyword in TypeScript.
185+
Refer back to the end of the lesson.
223186

224187
## --video-solution--
225188

226-
2
189+
3

0 commit comments

Comments
 (0)