mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-08-06 09:16:29 +02:00
Adding content to 115-type-assertions
This commit is contained in:
committed by
Kamran Ahmed
parent
1df4e4b836
commit
9edcb35acb
@@ -1 +1,17 @@
|
|||||||
# As const
|
# As Const
|
||||||
|
|
||||||
|
`as const` is a type assertion in TypeScript that allows you to assert that an expression has a specific type, and that its value should be treated as a read-only value.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```
|
||||||
|
const colors = ['red', 'green', 'blue'] as const;
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
Learn more from the following links:
|
||||||
|
|
||||||
|
- [const assertions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions)
|
@@ -1 +1,14 @@
|
|||||||
# As type
|
# As Type
|
||||||
|
|
||||||
|
as is a type assertion in TypeScript that allows you to tell the compiler to treat a value as a specific type, regardless of its inferred type.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```
|
||||||
|
let num = 42;
|
||||||
|
let str = num as string;
|
||||||
|
|
||||||
|
// 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.
|
@@ -1 +1,13 @@
|
|||||||
# As any
|
# As Any
|
||||||
|
|
||||||
|
`any` is a special type in TypeScript that represents a value of any type. When a value is declared with the any type, the compiler will not perform any type checks or type inference on that value.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```
|
||||||
|
let anyValue: any = 42;
|
||||||
|
|
||||||
|
// we can assign any value to anyValue, regardless of its type
|
||||||
|
anyValue = 'Hello, world!';
|
||||||
|
anyValue = true;
|
||||||
|
```
|
@@ -1 +1,18 @@
|
|||||||
# Non null assertion
|
# Non Null Assertion
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
```
|
||||||
|
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;
|
||||||
|
```
|
||||||
|
|
||||||
|
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`.
|
||||||
|
|
||||||
|
Learn more from the following links:
|
||||||
|
|
||||||
|
- [Non-null assertion operator](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator)
|
@@ -1 +1,26 @@
|
|||||||
# Type assertions
|
# Type Assertions
|
||||||
|
|
||||||
|
Type assertions in TypeScript are a way to tell the compiler to treat a value as a specific type, regardless of its inferred type.
|
||||||
|
|
||||||
|
There are two syntaxes for type assertions in TypeScript:
|
||||||
|
|
||||||
|
1. The "angle-bracket" syntax: <T>value
|
||||||
|
2. The "as" syntax: value as T
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```
|
||||||
|
let num = 42;
|
||||||
|
|
||||||
|
// using angle-bracket syntax
|
||||||
|
let str = <string>num;
|
||||||
|
|
||||||
|
// using as syntax
|
||||||
|
let str2 = num as string;
|
||||||
|
```
|
||||||
|
|
||||||
|
In both examples, `num` is a number, but the type assertions tell the compiler to treat the value as a string.
|
||||||
|
|
||||||
|
Learn more from the following links:
|
||||||
|
|
||||||
|
- [Type Assertions](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions)
|
Reference in New Issue
Block a user