From 26eaa40dc134c6e71255c09ad3ffafbd2b0cb8e0 Mon Sep 17 00:00:00 2001 From: syedmouaazfarrukh Date: Wed, 1 Feb 2023 05:05:23 -0800 Subject: [PATCH] Adding content to 109-generics --- .../content/109-generics/100-generic-types.md | 36 ++++++++++++++++++- .../109-generics/101-generic-constraints.md | 29 ++++++++++++++- .../typescript/content/109-generics/index.md | 21 ++++++++++- 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/src/roadmaps/typescript/content/109-generics/100-generic-types.md b/src/roadmaps/typescript/content/109-generics/100-generic-types.md index 7c0537197..9542adbba 100644 --- a/src/roadmaps/typescript/content/109-generics/100-generic-types.md +++ b/src/roadmaps/typescript/content/109-generics/100-generic-types.md @@ -1 +1,35 @@ -# Generic types \ No newline at end of file +# Generic Types + +Generic types in TypeScript allow you to write functions and classes that work with multiple data types, instead of being limited to a single data type. A generic type is defined using angle brackets and can be used as a placeholder for a specific data type. The actual data type is specified when the function or class is used. + +For example, the following is a generic function that takes a single argument of any data type and returns the same data type: + + ``` + function identity(arg: T): T { + return arg; + } + + let output = identity("Hello"); // type of output will be 'string' + ``` + +In this example, the `identity` function takes a single argument of any data type and returns the same data type. The actual data type is specified when the function is called by using `` before the argument `Hello`. + +Generics can also be used with classes, interfaces, and object types, allowing them to work with multiple data types as well. + +For example: + + ``` + class GenericNumber { + zeroValue: T; + add: (x: T, y: T) => T; + } + + let myGenericNumber = new GenericNumber(); + myGenericNumber.zeroValue = 0; + myGenericNumber.add = function(x, y) { return x + y; }; + ``` + +Learn more from the following resources: + +- [Generics - TypeScript](https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html#generics) +- [Typescript Generics Tutorial](https://www.youtube.com/watch?v=nViEqpgwxHE) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/109-generics/101-generic-constraints.md b/src/roadmaps/typescript/content/109-generics/101-generic-constraints.md index b1832a845..8795d5383 100644 --- a/src/roadmaps/typescript/content/109-generics/101-generic-constraints.md +++ b/src/roadmaps/typescript/content/109-generics/101-generic-constraints.md @@ -1 +1,28 @@ -# Generic constraints \ No newline at end of file +# Generic Constraints + +Generic constraints in TypeScript allow you to specify the requirements for the type parameters used in a generic type. These constraints ensure that the type parameter used in a generic type meets certain requirements. + +Constraints are specified using the extends keyword, followed by the type that the type parameter must extend or implement. + +For example: + + ``` + interface Lengthwise { + length: number; + } + + function loggingIdentity(arg: T): T { + console.log(arg.length); // Now we know it has a .length property, so no more error + return arg; + } + + loggingIdentity(3); // Error, number doesn't have a .length property + loggingIdentity({length: 10, value: 3}); // OK + ``` + +In this example, the `Lengthwise` interface defines a `length` property. The `loggingIdentity` function uses a generic type parameter `T` that is constrained by the `Lengthwise` interface, meaning that the type parameter must extend or implement the `Lengthwise` interface. This constraint ensures that the length property is available on the argument passed to the `loggingIdentity` function. + +Learn more from the following resources: + +- [Generic Constraints - TypeScript](https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-constraints) +- [TypeScript Course - Generics Constraints](https://www.youtube.com/watch?v=hLP2evgcAq4) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/109-generics/index.md b/src/roadmaps/typescript/content/109-generics/index.md index 380856fe4..f76b1a2f4 100644 --- a/src/roadmaps/typescript/content/109-generics/index.md +++ b/src/roadmaps/typescript/content/109-generics/index.md @@ -1 +1,20 @@ -# Generics \ No newline at end of file +# Generics + +Generics in TypeScript are a way to write code that can work with multiple data types, instead of being limited to a single data type. Generics allow you to write functions, classes, and interfaces that take one or more type parameters, which act as placeholders for the actual data types that will be used when the function, class, or interface is used. + +For example, the following is a generic function that takes a single argument of any data type and returns the same data type: + + ``` + function identity(arg: T): T { + return arg; + } + + let output = identity("Hello"); // type of output will be 'string' + ``` + +In this example, the `identity` function takes a single argument of any data type and returns the same data type. The actual data type is specified when the function is called by using `` before the argument `"Hello"`. + +Learn more from the following resources: + +- [Hello World of Generics](https://www.typescriptlang.org/docs/handbook/2/generics.html#hello-world-of-generics) +- [Typescript Generics Tutorial](https://www.youtube.com/watch?v=nViEqpgwxHE) \ No newline at end of file