From d96e5890b98e512104cd785597edf2062d098569 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Mon, 6 Feb 2023 21:26:16 +0000 Subject: [PATCH] Add content to TypeScript roadmap --- .../115-type-assertions/100-as-const.md | 8 ++++---- .../115-type-assertions/101-as-type.md | 10 +++++----- .../115-type-assertions/102-as-any.md | 12 ++++++------ .../115-type-assertions/103-non-null-assertion.md | 12 +++++------- 4 files changed, 20 insertions(+), 22 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 6800d1c29..1f248f3cd 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 @@ -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. 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 40cd2a22d..910f964cb 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 @@ -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. \ 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 f03c43184..517ca3581 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 @@ -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; - ``` \ No newline at end of file +// 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 531f9a247..c5b42211e 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 @@ -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`.