// 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.
@ -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.
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`.