-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from developedbysaad/update-scripts
Update L2 scripts
- Loading branch information
Showing
5 changed files
with
129 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 40 additions & 20 deletions
60
typescript-fundamentals/arrays-and-tuples-in-ts/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 36 additions & 14 deletions
50
typescript-fundamentals/datatypes-in-typescript/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,66 @@ | ||
In this lesson, we will learn about the `any`, `void` and `never` data types in TypeScript. The `any` data type is a special type that can be used to opt out of TypeScript's type-checking system. It's useful in cases where you don't know the type of value ahead of time, or where you want to disable type-checking for a specific variable or expression. The `void` and `never` data types are used to represent the absence of a value. | ||
In this lesson, we'll dive into TypeScript's data types. Specifically, we'll explore the `any`, `void`, and `never` data types and understand how they work. | ||
|
||
To use the `any` data type, you simply need to annotate the variable or expression with any. For example, try writing the following code in your `main.ts`: | ||
## Understanding `any` Data Type | ||
|
||
```js | ||
The `any` data type is a unique one. It's like a wildcard that allows us to escape TypeScript's type-checking system. This is handy when you don't know the type of a value in advance or when you intentionally want to bypass type-checking for a specific variable or expression. | ||
|
||
Let's see how to use the `any` data type in code. | ||
|
||
> Action: Write the following code in `main.ts` file: | ||
```javascript | ||
let name: any = "hello"; | ||
name = 42; | ||
name = false; | ||
``` | ||
|
||
In this example, the variable `name` is declared as type `any`, which means it can hold any type of value. As a result, we can assign it a `string`, a `number`, and a `boolean` value without any errors being thrown. | ||
In this code, we declare the `name` variable with the type `any`. This means it can hold values of any type, be it a string, number, or boolean. TypeScript won't complain, even though we're switching data types. | ||
|
||
## Exploring the `void` Data Type | ||
|
||
Next up, we have the `void` data type. It represents the absence of a value and is often used as the return type of functions that don't return anything. | ||
|
||
Let's add a `void` function to our code | ||
|
||
The `void` type represents the absence of any type. It is commonly used as the return type of function that does not return a value. Add the below code to your `main.ts` file: | ||
> Action: Add the below code to your main.ts file: | ||
```js | ||
```javascript | ||
function printHello(): void { | ||
console.log("Hello!"); | ||
} | ||
``` | ||
|
||
In this example, the `printHello` function does not return a value, so its return type is `void`. If we try to return a value from this function, TypeScript will give us an error: | ||
In this example, the `printHello` function doesn't return any value, so we mark it with the `void` type. If you try to return something from a `void` function, TypeScript will raise an error. | ||
|
||
```js | ||
```javascript | ||
function printHello(): void { | ||
console.log("Hello!"); | ||
return "hello"; // TypeScript will give an error here | ||
} | ||
``` | ||
|
||
The `never` type represents the absence of a value, but it indicates that the function will never return. This is useful for representing functions that throw an error or never terminate. Add the below code to your `main.ts` file: | ||
## The `never` Data Type | ||
|
||
```js | ||
Lastly, we have the `never` data type. It signifies the absence of a value and implies that a function will never return. This is particularly useful for functions that either throw errors or run infinitely. | ||
|
||
Let's add a function using the `never` type. | ||
|
||
> Action: Paste the following snippet of code into your `main.ts` file. | ||
```javascript | ||
function throwError(): never { | ||
throw new Error("An error occurred!"); | ||
} | ||
``` | ||
|
||
In this example, the `throwError` function never returns because it always throws an error. Therefore, its return type is `never`. | ||
In the `throwError` function, it's clear that it will never return normally because it always throws an error. Therefore, its return type is `never`. | ||
|
||
## Important Considerations | ||
|
||
Keep in mind that while the `any` data type is versatile, it can also compromise the benefits of using TypeScript, such as improved code reliability and maintainability. Thus, it's advisable to use it sparingly, only when absolutely necessary. | ||
|
||
It's important to note that using the `any` data type can disable many of the benefits of using TypeScript, such as improved code reliability and better code maintainability. As a result, it's generally best to restrict the use of `any` data type to a minimum and only when absolutely necessary. | ||
By understanding and using these data types in TypeScript, you can catch errors early, ensuring that your code functions as intended and is more robust. | ||
|
||
Using these types in TypeScript can help you catch errors in your code and ensure that your functions are working as intended. | ||
## Conclusion | ||
|
||
See you in the next lesson! | ||
That's it for this lesson! You've now learned about the `any`, `void`, and `never` data types in TypeScript. These concepts are fundamental to writing type-safe and reliable code in your ReactJS and TypeScript projects. Stay tuned for the next lesson! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 20 additions & 14 deletions
34
typescript-fundamentals/type-annotations-and-inference/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,41 @@ | ||
In this lesson, we'll cover what type annotations and inference are and how they are used in TypeScript. | ||
In this lesson, we'll explore two essential concepts in TypeScript: `Type Annotations` and `Type Inference`, and understand how they are used to make your code more robust and self-documenting. | ||
|
||
TypeScript being a superset of JavaScript introduces several features that are not available in JavaScript, such as static typing and type annotations. | ||
## Type Annotations | ||
|
||
Type annotations allow you to specify the expected data type of the function's arguments and return value. This can help make your code more self-documenting and prevent certain types of runtime errors. | ||
Type Annotations in TypeScript allow you to explicitly specify the expected data types of function arguments and return values. This not only makes your code more self-documenting but also helps prevent certain types of runtime errors. | ||
|
||
For example, let's consider the following function that takes in the details of a user and returns a success message as a string. Add the below code to the `main.ts` where we have the interface for the User defined: | ||
Let's consider an example with type annotations. | ||
|
||
```js | ||
> Action: Paste the following snippet of code in `main.ts` file. | ||
```javascript | ||
function addUser(user: User): string { | ||
return user.name + " added successfully"; | ||
} | ||
``` | ||
|
||
Here, we have used type annotations to specify that the `addUser` function takes in an input which follows the interface User and returns a string. | ||
In this code, we've used type annotations to declare that the `addUser` function expects an argument following the `User` interface and will return a `string`. This clarity in our code helps us catch type-related issues early. | ||
|
||
Type annotations are optional in TypeScript, and you can use them even if you don't have a type-checker installed. You can perform static type-checking to ensure that your code is type-safe. | ||
Type annotations are optional in TypeScript, even without a type-checker installed. You can still use them for static type-checking, ensuring your code is type-safe. | ||
|
||
Now, let's move on to type inference. | ||
## Type Inference | ||
|
||
In TypeScript, type inference refers to the ability of the compiler to automatically determine the data type of a value based on its usage. This means that you don't have to explicitly specify the types of variables and expressions in your code, and the compiler will infer their types for you. | ||
Type Inference in TypeScript refers to the compiler's ability to automatically deduce the data type of a value based on its usage. This means you don't always need to explicitly specify variable or expression types; the compiler can figure them out for you. | ||
|
||
For example, consider the following code: | ||
> Action: Let's see type inference in action. | ||
```js | ||
```javascript | ||
let userName = "Jane"; // type: string | ||
let userID = 10; // type: number | ||
let uniqueID = userName + userID; | ||
``` | ||
|
||
Here, the compiler will automatically infer that `userName` is a string and `userID` is a number and will concatenate the final output as a string. | ||
In this code, the TypeScript compiler automatically infers that `userName` is of type `string`, `userID` is of type `number`, and `uniqueID` becomes a string. This simplifies your code while ensuring type safety. | ||
|
||
## Combining Annotations and Inference | ||
|
||
By combining Type Annotations and Type Inference, you can write clean, self-documenting code that's also type-safe. Use annotations where clarity is essential, and let inference do the heavy lifting when types are obvious. | ||
|
||
By using type annotations and type inference together, you can write clean, self-documenting code that is also type-safe. | ||
## Conclusion | ||
|
||
See you in the next lesson! | ||
That wraps up our lesson on TypeScript Type Annotations and Inference. These concepts are fundamental for writing type-safe and clear code in your ReactJS and TypeScript projects. Stay tuned for the next lesson where we'll dive into more TypeScript goodness! |