diff --git a/src/roadmaps/typescript/content/102-type-inference.md b/src/roadmaps/typescript/content/102-type-inference.md index b38016d0d..e70debd21 100644 --- a/src/roadmaps/typescript/content/102-type-inference.md +++ b/src/roadmaps/typescript/content/102-type-inference.md @@ -1 +1,16 @@ -# Type inference \ No newline at end of file +# Type Inference + +Type inference in TypeScript refers to the process of automatically determining the type of a variable based on the value assigned to it. This allows you to write code that is more concise and easier to understand, as the TypeScript compiler can deduce the types of variables without you having to explicitly specify them. + +Here's an example of type inference in TypeScript: + +``` +let name = "John Doe"; +``` + +In this example, the TypeScript compiler automatically infers that the type of the name variable is string. This means that you can use the name variable just like any other string in your code, and the TypeScript compiler will ensure that you don't perform any invalid operations on it. + +Learn more from the following links: + +- [Type Inference](https://www.typescriptlang.org/docs/handbook/type-inference.html#handbook-content) +- [Type Inference in TypeScript](https://www.tutorialsteacher.com/typescript/type-inference) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/103-type-compatibility.md b/src/roadmaps/typescript/content/103-type-compatibility.md index 5f4b661a4..11910dea4 100644 --- a/src/roadmaps/typescript/content/103-type-compatibility.md +++ b/src/roadmaps/typescript/content/103-type-compatibility.md @@ -1 +1,24 @@ -# Type compatibility \ No newline at end of file +# Type Compatibility + +Type compatibility in TypeScript refers to the compatibility between different types in TypeScript. TypeScript uses structural typing to determine type compatibility. This means that two types are considered compatible if they have the same structure, regardless of their names. + +Here's an example of type compatibility in TypeScript: + + ``` + interface Point { + x: number; + y: number; + } + + let p1: Point = { x: 10, y: 20 }; + let p2: { x: number; y: number } = p1; + + console.log(p2.x); // Output: 10 + ``` + +In this example, `p1` has the type `Point`, while `p2` has the type `{ x: number; y: number }`. Despite the fact that the two types have different names, they are considered compatible because they have the same structure. This means that you can assign a value of type `Point` to a variable of type `{ x: number; y: number }`, as we do with `p1` and `p2` in this example. + +Learn more from the following links: + +- [Type Compatibility](https://www.typescriptlang.org/docs/handbook/type-compatibility.html) +- [Tutorial - Type Compatibility in TypeScript](youtube.com/watch?v=wqm5ibtCSf0) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/110-decorators.md b/src/roadmaps/typescript/content/110-decorators.md index cdf44496f..c780a5129 100644 --- a/src/roadmaps/typescript/content/110-decorators.md +++ b/src/roadmaps/typescript/content/110-decorators.md @@ -1 +1,34 @@ -# Decorators \ No newline at end of file +# Decorators + +Decorators are a feature of TypeScript that allow you to modify the behavior of a class, property, method, or parameter. They are a way to add additional functionality to existing code, and they can be used for a wide range of tasks, including logging, performance optimization, and validation. + +Here's an example of how you might use a decorator in TypeScript: + + ``` + function log(target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) { + const originalMethod = descriptor.value; + descriptor.value = function (...args: any[]) { + console.log(`Calling ${propertyKey} with arguments: ${args}`); + return originalMethod.apply(this, args); + }; + return descriptor; + } + + class Calculator { + @log + add(a: number, b: number): number { + return a + b; + } + } + + const calculator = new Calculator(); + calculator.add(1, 2); + // Output: Calling add with arguments: 1,2 + // Output: 3 + ``` + +In this example, we use the `@log` decorator to modify the behavior of the `add` method in the `Calculator` class. The `log` decorator logs the arguments passed to the method before calling the original method. This allows us to see what arguments are being passed to the method, without having to modify the method's code. + +Learn more from the following links: + +- [Decorators](https://www.typescriptlang.org/docs/handbook/decorators.html#handbook-content) \ No newline at end of file