1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-08-07 09:48:12 +02:00

Add content to TypeScript roadmap

This commit is contained in:
Kamran Ahmed
2023-02-06 21:26:16 +00:00
parent 659bd93094
commit d96e5890b9
4 changed files with 20 additions and 22 deletions

View File

@@ -4,11 +4,11 @@
For example: For example:
``` ```typescript
const colors = ['red', 'green', 'blue'] as const; const colors = ['red', 'green', 'blue'] as const;
// colors is now of type readonly ['red', 'green', 'blue'] // colors is now of type readonly ['red', 'green', 'blue']
``` ```
Using as const allows TypeScript to infer more accurate types for constants, which can lead to improved type checking and better type inference in your code. Using as const allows TypeScript to infer more accurate types for constants, which can lead to improved type checking and better type inference in your code.

View File

@@ -4,11 +4,11 @@ as is a type assertion in TypeScript that allows you to tell the compiler to tre
For example: For example:
``` ```typescript
let num = 42; let num = 42;
let str = num as string; let str = num as string;
// str is now of type string, even though num is a number // str is now of type string, even though num is a number
``` ```
It's important to note that type assertions do not change the runtime type of a value, and do not cause any type of conversion. They simply provide a way for the programmer to override the type inference performed by the compiler. It's important to note that type assertions do not change the runtime type of a value, and do not cause any type of conversion. They simply provide a way for the programmer to override the type inference performed by the compiler.

View File

@@ -4,10 +4,10 @@
For example: For example:
``` ```typescript
let anyValue: any = 42; let anyValue: any = 42;
// we can assign any value to anyValue, regardless of its type // we can assign any value to anyValue, regardless of its type
anyValue = 'Hello, world!'; anyValue = 'Hello, world!';
anyValue = true; anyValue = true;
``` ```

View File

@@ -2,14 +2,12 @@
The non-null assertion operator (!) is a type assertion in TypeScript that allows you to tell the compiler that a value will never be null or undefined. The non-null assertion operator (!) is a type assertion in TypeScript that allows you to tell the compiler that a value will never be null or undefined.
For example: ```typescript
let name: string | null = null;
``` // we use the non-null assertion operator to tell the compiler that name will never be null
let name: string | null = null; let nameLength = name!.length;
```
// we use the non-null assertion operator to tell the compiler that name will never be null
let nameLength = name!.length;
```
The non-null assertion operator is used to assert that a value is not null or undefined, and to tell the compiler to treat the value as non-nullable. However, it's important to be careful when using the non-null assertion operator, as it can lead to runtime errors if the value is actually `null` or `undefined`. The non-null assertion operator is used to assert that a value is not null or undefined, and to tell the compiler to treat the value as non-nullable. However, it's important to be careful when using the non-null assertion operator, as it can lead to runtime errors if the value is actually `null` or `undefined`.