From 9edcb35acb1c49dc8db798ffc8b08b5f28da08d4 Mon Sep 17 00:00:00 2001 From: syedmouaazfarrukh Date: Wed, 1 Feb 2023 08:35:46 -0800 Subject: [PATCH] Adding content to 115-type-assertions --- .../115-type-assertions/100-as-const.md | 18 ++++++++++++- .../115-type-assertions/101-as-type.md | 15 ++++++++++- .../115-type-assertions/102-as-any.md | 14 +++++++++- .../103-non-null-assertion.md | 19 ++++++++++++- .../115-type-assertions/index.md | 27 ++++++++++++++++++- 5 files changed, 88 insertions(+), 5 deletions(-) diff --git a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/100-as-const.md b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/100-as-const.md index 364306081..6800d1c29 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/100-as-const.md +++ b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/100-as-const.md @@ -1 +1,17 @@ -# As const \ No newline at end of file +# 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) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/101-as-type.md b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/101-as-type.md index 3d58d46ea..40cd2a22d 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/101-as-type.md +++ b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/101-as-type.md @@ -1 +1,14 @@ -# As type \ No newline at end of file +# 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. \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/102-as-any.md b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/102-as-any.md index ac203428e..f03c43184 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/102-as-any.md +++ b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/102-as-any.md @@ -1 +1,13 @@ -# As any \ No newline at end of file +# 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; + ``` \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/103-non-null-assertion.md b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/103-non-null-assertion.md index 4e97fbb37..531f9a247 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/103-non-null-assertion.md +++ b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/103-non-null-assertion.md @@ -1 +1,18 @@ -# Non null assertion \ No newline at end of file +# 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) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/index.md b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/index.md index 0cd2ea142..d3953193d 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/index.md +++ b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/index.md @@ -1 +1,26 @@ -# Type assertions \ No newline at end of file +# 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: value +2. The "as" syntax: value as T + +For example: + + ``` + let num = 42; + + // using angle-bracket syntax + let str = 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) \ No newline at end of file