Skip to content

Commit c871be5

Browse files
committed
Fixed md code annotations
1 parent 972e5a9 commit c871be5

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

module8-typescript/r1-introduction-to-typescript/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ console.log(user.name)
2929

3030
Actually, TypeScript won't even allow you to run this code: you'll get an error.
3131

32-
```bash
32+
```
3333
Property 'name' does not exist on type '{ firstName: string; lastName: string; role: string; }'.
3434
```
3535

@@ -89,7 +89,7 @@ Generally speaking, it is best not to form any kind of unshakable opinions about
8989

9090
We've learned quite a bit about TypeScript and why it makes JavaScript even better. Let's understand this in action with frontend and backend code. TypeScript is compatible with most modern frontend frameworks like React or Next.js. If you're familiar with JSX in React, to use TypeScript you would create a `.tsx` file instead. Let's look at an example of a `Header.tsx` file.
9191

92-
```js
92+
```tsx
9393
export interface Props {
9494
title: string
9595
color?: string
@@ -109,7 +109,7 @@ Here we have created a Header component that takes in two props `title` and `col
109109
110110
So now when we want to have a Header component on a page:
111111
112-
```js
112+
```tsx
113113
<Header />
114114
<Header title='Hello World' />
115115
<Header title='Hello World' color='red' />
@@ -119,10 +119,10 @@ The first case will give us an error within our IDE stating that the prop title
119119
120120
TypeScript is also very compatible on the backend, you can have `.ts` files in your Node Express projects or use a backend framework like NestJS which has TypeScript incorporated. We will learn more about this framework in an upcoming lesson, let's take a look at another TypeScript example useful for backend.
121121
122-
```js
122+
```ts
123123
type User = {
124-
name: string,
125-
age: number,
124+
name: string;
125+
age: number;
126126
};
127127

128128
function isAdult(user: User): boolean {

module8-typescript/r1.1-everyday-types/README.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ We'll start by reviewing the most basic and common types you might encounter whe
1111

1212
When you declare a variable using `const`, `var`, or `let`, you can optionally add a type annotation to explicitly specify the type of the variable:
1313

14-
```typescript
14+
```ts
1515
let myName: string = "Alice";
1616
```
1717

1818
TypeScript doesn't use "types on the left"-style declarations like `int x = 0`; Type annotations will always go after the thing being typed.
1919

2020
In most cases, though, this isn’t needed. Wherever possible, TypeScript tries to automatically infer the types in your code. For example, the type of a variable is inferred based on the type of its initializer:
2121

22-
```typescript
22+
```ts
2323
// No type annotation needed -- 'myName' inferred as type 'string'
2424
let myName = "Alice";
2525
```
@@ -28,7 +28,7 @@ let myName = "Alice";
2828

2929
JavaScript has three primitive types -- `string`, `number`, `boolean` -- and they are present in TypeScript as well. You can declare them just as you would in JavaScript. However, in TypeScript, you may _optionally_ add a type. As mentioned previously, in most cases TypeScript can infer the type correctly.
3030

31-
```typescript
31+
```ts
3232
// TypeScript infers the type.
3333
const name = "Foo Bar";
3434
const age = 36;
@@ -44,7 +44,7 @@ const isInBootcamp: boolean = true;
4444

4545
What does it mean when TypeScript is type-safe? Let's try adding together a number and a string in both JavaScript and TypeScript:
4646

47-
```typescript
47+
```ts
4848
// JavaScript: prints "1foo"
4949
console.log(1 + "foo");
5050

@@ -62,7 +62,7 @@ We'll go through a few examples.
6262

6363
TypeScript will infer the type of an array if it is declared with some value initially. For example, we have an array of numbers below; TypeScript is smart enough to figure out that `ids` has the type `number[]`.
6464

65-
```typescript
65+
```ts
6666
const ids = [1, 2, 3];
6767
ids.push("somestring");
6868
```
@@ -77,7 +77,7 @@ Argument of type 'string' is not assignable to parameter of type 'number'.
7777

7878
If there is no initial value, TypeScript is, unfortunately, not smart enough to figure out the type of an array. For example:
7979

80-
```typescript
80+
```ts
8181
// No type is declared on the array. TypeScript doesn't know
8282
// what type the members are. This code passes, and anything
8383
// can be pushed into the array (not very helpful for type safety).
@@ -91,7 +91,7 @@ TypeScript believes that the members of the array can be any type (formally: typ
9191

9292
If you plan on declaring an empty array, this is an instance where you would want to explicitly specify the type in order to receive type-safety:
9393

94-
```typescript
94+
```ts
9595
// This will give an error if you try to push a string to the array this time.
9696
const ids: number[] = [];
9797
ids.push(5);
@@ -103,14 +103,14 @@ The `number[]` indicates that this will be an array of numbers. Similarly, you c
103103

104104
TypeScript also has a special type, `any`, that you can use whenever you don't want a particular value to cause type-checking errors. It is useful when you don't want to write out a long type just to convince TypeScript that a particular line of code is okay.
105105

106-
```typescript
106+
```ts
107107
let x: any = "Hello";
108108
let arr: any[] = [1, true, "Hello"];
109109
```
110110

111111
When a value is of type `any`, you can access any properties of it (which will, in turn, be of type `any`), call it like a function, assign it to (or from) a value of any type, or pretty much anything else that’s syntactically legal.
112112

113-
```typescript
113+
```ts
114114
let obj: any = { x: 0 };
115115
// None of the following lines of code will throw compiler errors.
116116
// Using `any` disables all further type checking, and it is assumed
@@ -128,7 +128,7 @@ When the keyword `any` is used, TypeScript no longer performs type checking, sin
128128

129129
Here is an example of typing something as `any`, demonstrating how we can start introducing errors in our code that were previously prevented:
130130

131-
```typescript
131+
```ts
132132
const a: any = [1, 2, 3, 4, 5];
133133
const invalidAddition = a + 5; // Adding a list to a number?
134134
const functionDoesntExist = a.someInvalidFunction(); // This also compiles!
@@ -140,7 +140,7 @@ Some valid usages of `any` might be: facilitating code migration from JavaScript
140140

141141
Sometimes you may have an array whose elements are of different types. This is usually not encouraged, but it is possible. TypeScript allows type definitions for mixed arrays or tuples. You could even have an array of tuples.
142142

143-
```typescript
143+
```ts
144144
// Tuple
145145
let person: [number, string, boolean] = [1, "Alice", true];
146146
// Tuple Array
@@ -154,7 +154,7 @@ employees = [
154154

155155
If you're wondering how is a tuple different from an `any[]`, tuples have a fixed type for each element even if not the same type for all elements. But when you state an array of `any` there could be as many different elements of different types.
156156

157-
```typescript
157+
```ts
158158
// This tuple would give an error because the third element
159159
//is expected to be a boolean but is assigned a string
160160
let employee: [number, string, boolean] = [1, "Alice", "Developer"];
@@ -170,7 +170,7 @@ manager = [false, "Hello"];
170170

171171
Functions are the primary means of passing data around in JavaScript. TypeScript allows you to specify the types of both the input and output values of functions. In TypeScript, you can also use both the `function` keyword or arrow functions, but you must add types to the parameters. And where required you can add return type annotations too.
172172

173-
```typescript
173+
```ts
174174
// Function keyword
175175
function add(x: number, y: number): number {
176176
return x + y;
@@ -186,7 +186,7 @@ Note the addition of three number types: the two parameters `x`, `y`, and the re
186186

187187
It should be noted that in TypeScript every parameter is required unless specified by adding a `?` after the parameter. In JavaScript, if a function has three declared parameters, you can choose not to provide them, and they will take the value of `undefined`. This is not the case in TypeScript.
188188

189-
```typescript
189+
```ts
190190
// Return both the first name and last name if the last name is provided,
191191
// otherwise return only the first name.
192192
function makeName(firstName: string, lastName?: string) {
@@ -204,7 +204,7 @@ In the example above, the second parameter, `lastName`, is optional. This is bec
204204

205205
Anonymous functions are a little bit different from function declarations. When a function appears in a place where TypeScript can determine how it's going to be called, the parameters of that function are automatically given types.
206206

207-
```typescript
207+
```ts
208208
// No type annotations here, but TypeScript can spot the bug
209209
const names = ["Alice", "Bob", "Eve"];
210210

@@ -225,7 +225,7 @@ We learned how to write types on a function, but it's very common in JavaScript
225225

226226
Consider a function that takes another function that operates on numbers (consider this a version of `map`, that, for the sake of example, only works on numbers).
227227

228-
```typescript
228+
```ts
229229
// Takes a list of numbers and applies a processor function to each number
230230
function mapNumbers(nums: number[], fn: (x: number): number) {
231231
return nums.map(fn);
@@ -238,17 +238,17 @@ Note the syntax here to declare a function type. You must add the parameter name
238238

239239
A union type is a type formed from two or more other types, representing values that may be any one of those types. We refer to each of these types as the union's members.
240240

241-
```typescript
241+
```ts
242242
let id: string | number;
243243
// This is valid
244244
id = 22;
245245
// This is also valid
246246
id = "22";
247247
```
248248

249-
It's easy to provide a value matching a union type - simply provide a type matching any of the union's members. If you have a value of a union type, how do you work with it? TypeScript will only allow an operation if it is valid for every member of the union. For example, if you have the union `string | number`, you can`t use methods that are only available on `string`:
249+
It's easy to provide a value matching a union type - simply provide a type matching any of the union's members. If you have a value of a union type, how do you work with it? TypeScript will only allow an operation if it is valid for every member of the union. For example, if you have the union `string | number`, you can't use methods that are only available on `string`:
250250

251-
```typescript
251+
```ts
252252
function printId(id: number | string) {
253253
console.log(id.toUpperCase());
254254
Property 'toUpperCase' does not exist on type 'string | number'.
@@ -258,7 +258,7 @@ Property 'toUpperCase' does not exist on type 'string | number'.
258258

259259
The solution is to narrow the union with code, the same as you would in JavaScript without type annotations. Narrowing occurs when TypeScript can deduce a more specific type for a value based on the structure of the code. For example, TypeScript knows that only a `string` value will have a `typeof` value `"string"`:
260260

261-
```typescript
261+
```ts
262262
function printId(id: number | string) {
263263
if (typeof id === "string") {
264264
// In this branch, id is of type 'string'
@@ -276,7 +276,7 @@ Apart from primitives, the most common sort of type you'll encounter is an objec
276276

277277
For example, here's a function that takes a point-like object:
278278

279-
```typescript
279+
```ts
280280
// The parameter's type annotation is an object type
281281
function printCoord(pt: { x: number; y: number }) {
282282
console.log("The coordinate's x value is " + pt.x);
@@ -289,7 +289,7 @@ Here, we annotated the parameter with a type with two properties - `x` and `y` -
289289

290290
Object types can also specify that some or all of their properties are optional. To do this, add a `?` after the property name:
291291

292-
```typescript
292+
```ts
293293
function printName(obj: { first: string; last?: string }) {
294294
// ...
295295
}
@@ -300,7 +300,7 @@ printName({ first: "Alice", last: "Alisson" });
300300

301301
In JavaScript, if you access a property that doesn't exist, you'll get the value undefined rather than a runtime error. Because of this, when you read from an optional property, you'll have to check for undefined before using it.
302302

303-
```typescript
303+
```ts
304304
function printName(obj: { first: string; last?: string }) {
305305
// Error - might crash if 'obj.last' wasn't provided!
306306
console.log(obj.last.toUpperCase());
@@ -319,7 +319,7 @@ function printName(obj: { first: string; last?: string }) {
319319

320320
We've been using object types and union types by writing them directly in type annotations. This is convenient, but it's common to want to use the same type more than once and refer to it by a single name. A type alias is exactly that - a name for any type. The syntax for a type alias is:
321321

322-
```typescript
322+
```ts
323323
type Point = {
324324
x: number;
325325
y: number;
@@ -341,7 +341,7 @@ With `strictNullChecks` off, values that might be `null` or `undefined` can stil
341341

342342
With `strictNullChecks` on, when a value is `null` or `undefined`, you will need to test for those values before using methods or properties on that value. Just like checking for `undefined` before using an optional property, we can use narrowing to check for values that might be null:
343343

344-
```typescript
344+
```ts
345345
function doSomething(x: string | null) {
346346
if (x === null) {
347347
// do nothing
@@ -353,7 +353,7 @@ function doSomething(x: string | null) {
353353

354354
TypeScript also has a special syntax for removing `null` and `undefined` from a type without doing any explicit checking. Writing `!` after any expression is effectively a type assertion that the value isn't `null` or `undefined`:
355355

356-
```typescript
356+
```ts
357357
function liveDangerously(x?: number | null) {
358358
// No error
359359
console.log(x!.toFixed());
@@ -364,7 +364,7 @@ function liveDangerously(x?: number | null) {
364364

365365
Enums are a feature added to JavaScript by TypeScript which allows for describing a value that could be one of a set of possible named constants. Unlike most TypeScript features, this is not a type-level addition to JavaScript but something added to the language and runtime. Because of this, it's a feature that you should know exists, but maybe hold off on using it unless you are sure.
366366

367-
```typescript
367+
```ts
368368
// Up is initialized with 1
369369
// All of the following members are auto-incremented from that point on
370370
// Up = 1 Down = 2 Left = 3 Right = 4

module8-typescript/r1.2-advanced-types/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ We have learned quite a bit about Types so far, but when it comes to large-scale
99

1010
An interface declaration is another way to name an object type. Interfaces can be used interchangeably with type aliases and simple object types.
1111

12-
```typescript
12+
```ts
1313
interface Point {
1414
x: number;
1515
y: number;
@@ -27,7 +27,7 @@ Like when we used a type alias above, the example works just as if we had used a
2727

2828
You can also define properties in an interface to be optional using `?` or non-editable by specifying `readonly`.
2929

30-
```typescript
30+
```ts
3131
interface UserInterface {
3232
readonly id: number;
3333
name: string;
@@ -42,7 +42,7 @@ const user1: UserInterface = {
4242

4343
Interfaces can be used with functions as well.
4444

45-
```typescript
45+
```ts
4646
interface MathFunc {
4747
(x: number, y: number): number;
4848
}
@@ -55,7 +55,7 @@ const sub: MathFunc = (x: number, y: number): number => x - y;
5555

5656
TypeScript offers full support for the class keyword introduced in ES2015. As with other JavaScript language features, TypeScript adds type annotations and other syntax features to allow you to express relationships between classes and other types. Just like any other OOP language, classes have members or fields and a constructor function is used to instantiate the objects of this class with initial field values. Classes can also have member functions.
5757

58-
```typescript
58+
```ts
5959
// Class with constructor
6060
class Person {
6161
id: number;
@@ -77,7 +77,7 @@ alice.greet("Hello");
7777

7878
You can use an `implements` clause to check that a class satisfies a particular interface.
7979

80-
```typescript
80+
```ts
8181
interface PersonInterface {
8282
id: number;
8383
name: string;
@@ -107,7 +107,7 @@ mike.register();
107107

108108
Classes may extend from a base class. A derived class has all the properties and methods of its base class, and also defines additional members.
109109

110-
```typescript
110+
```ts
111111
class Employee extends Person {
112112
position: string; // additional member of this subclass
113113

@@ -139,7 +139,7 @@ We've tried to cover the basics of Classes here. You can read a lot more in deta
139139

140140
Generics allow us to create reusable and flexible components which can work over a variety of types rather than a single one. This allows users to consume these components and use their types.
141141

142-
```typescript
142+
```ts
143143
// Without generics
144144
function getArray(items: any[]): any[] {
145145
return new Array().concat(items);
@@ -167,7 +167,7 @@ With the introduction of Classes in TypeScript and ES6, there now exist certain
167167

168168
A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use the form `@expression`, where `expression` must be a function that will be called at runtime with information about the decorated declaration. For example, given the decorator `@sealed` we might write the `sealed` function as follows:
169169

170-
```typescript
170+
```ts
171171
function sealed(target) {
172172
// do something with 'target' ...
173173
}

module8-typescript/r2-typescript-node-express/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Now if we run `npm run dev` we should see our Express server running in watch mo
9292

9393
Now let's add in some meaningful types in our Express server code. We'll use the custom Express types - Application, Request, Response, and NextFunction.
9494

95-
```typescript
95+
```ts
9696
import express, { Application, Request, Response, NextFunction } from "express";
9797

9898
const app: Application = express();
@@ -106,7 +106,7 @@ app.listen(5000, () => console.log("Server running"));
106106

107107
And then we can also have any functions with types that can be called in the handler functions for our endpoints.
108108

109-
```typescript
109+
```ts
110110
const add = (x: number, y: number): number => x + y;
111111
```
112112

module8-typescript/r2.1-backend-frameworks-typescript/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ TypeORM leverages TypeScript to write database integration code and is compatibl
8282

8383
With TypeORM your models look like this:
8484

85-
```typescript
85+
```ts
8686
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
8787

8888
@Entity()

0 commit comments

Comments
 (0)