Add content to TypeScript roadmap

pull/3423/head
Kamran Ahmed 2 years ago
parent 659bd93094
commit d96e5890b9
  1. 8
      src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/100-as-const.md
  2. 10
      src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/101-as-type.md
  3. 12
      src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/102-as-any.md
  4. 12
      src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/103-non-null-assertion.md

@ -4,11 +4,11 @@
For example:
```
const colors = ['red', 'green', 'blue'] as const;
```typescript
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.

@ -4,11 +4,11 @@ as is a type assertion in TypeScript that allows you to tell the compiler to tre
For example:
```
let num = 42;
let str = num as string;
```typescript
let num = 42;
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.

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

@ -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.
For example:
```typescript
let name: string | null = null;
```
let name: string | null = null;
// we use the non-null assertion operator to tell the compiler that name will never be 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`.

Loading…
Cancel
Save