mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-03-22 16:19:46 +01:00
Add content to TypeScript roadmap
This commit is contained in:
parent
659bd93094
commit
d96e5890b9
@ -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…
x
Reference in New Issue
Block a user