### Introduction to TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Serves as a starting point for learning TypeScript, outlining key concepts and benefits of using TypeScript. ```typescript // 21. Starting Point for Learning TypeScript ``` -------------------------------- ### Object.assign Example (JavaScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc_%20%E3%80%8AJS%EF%BC%88TS%EF%BC%89%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E3%80%8B Provides an example of using `Object.assign()` in JavaScript. This method copies all enumerable own properties from one or more source objects to a target object. It returns the target object. ```javascript const target = { a: 1, b: 2 }; const source1 = { b: 3, c: 4 }; const source2 = { d: 5 }; const result = Object.assign(target, source1, source2); console.log(result); // { a: 1, b: 3, c: 4, d: 5 } console.log(target); // { a: 1, b: 3, c: 4, d: 5 } ``` -------------------------------- ### Conditional Types in TypeScript (Documentation) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Provides documentation and examples for conditional types in TypeScript, explaining their syntax and how they are used for creating types that vary based on conditions. ```typescript // 20. Documentation - Conditional Types ``` -------------------------------- ### TypeScript Destructuring Assignment Examples Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=en-us&singleDoc= Provides examples of destructuring assignment in TypeScript for both object properties and array elements within function parameters. This enables concise extraction of values from complex data structures. ```typescript type C = { a: string, b: number } function f({ a, b }: C): void { // ... } ``` ```typescript function f([first, second]: [number, number]) { console.log(first); console.log(second); } // Assuming 'input' is defined elsewhere and matches the type [number, number] // f(input); ``` -------------------------------- ### Conditional Types in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Details conditional types in TypeScript, providing examples of how they work similarly to ternary operators but at the type level. ```typescript // 16. Conditional Types ``` -------------------------------- ### Using typeof Operator in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Demonstrates the use of the `typeof` operator in TypeScript, which can be used in two main contexts: in JavaScript code to get the type of a variable as a string, and in TypeScript type positions to refer to the type of a variable. ```typescript // 18. typeof ``` -------------------------------- ### Destructuring Assignment in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Provides an example of destructuring assignment in TypeScript, specifically within a function declaration. This allows for convenient extraction of properties from objects. ```typescript type C = { a: string, b: number } function f({ a, b }: C): void { // ... } ``` -------------------------------- ### Destructuring Assignment (TypeScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc= Shows examples of destructuring assignment in TypeScript for both object properties and array elements within function parameters. This allows for extracting values from complex data structures into distinct variables. ```typescript type C = { a: string, b: number } function f({ a, b }: C): void { // ... } function f([first, second]: [number, number]) { console.log(first); console.log(second); } // Assuming 'input' is defined elsewhere and matches the [number, number] type // f(input); ``` -------------------------------- ### Void 0 Usage (JavaScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc_%20%E3%80%8AJS%EF%BC%88TS%EF%BC%89%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E3%80%8B Explains the usage of `void 0` in JavaScript as a more robust way to get the `undefined` value compared to the global `undefined` variable, which can be reassigned in older JavaScript environments. `void 0` always evaluates to `undefined`. ```javascript // In modern JavaScript, undefined is a property of the global object and cannot be reassigned. // However, in older environments, it was possible. // Using void 0 for a reliable undefined value: function checkValue() { if (someVariable === void 0) { console.log('Variable is undefined'); } } let someVariable; checkValue(); // Output: Variable is undefined ``` -------------------------------- ### Working with Promises in JavaScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Provides resources and context for understanding Promises, async, and await in JavaScript for asynchronous programming. This covers fundamental patterns for handling asynchronous operations. ```javascript // 19. Promise // Asynchronous Programming: Understand Promise, async, await Once and For All (#js #javascript)_bilibili_bilibili ``` -------------------------------- ### Untitled Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= No description -------------------------------- ### Understanding Promises in JavaScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Provides context and resources for understanding JavaScript Promises, async, and await for asynchronous operations. ```javascript // 19. Promise ``` -------------------------------- ### Rules for Writing Excellent Passable Functions in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Discusses best practices and rules for writing high-quality, type-safe functions in TypeScript that are easily understandable and maintainable. ```typescript // 15. Rules for Writing Excellent Passable Functions ``` -------------------------------- ### Imitating StyleSheet.create() in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Provides a TypeScript snippet that imitates the functionality of `StyleSheet.create()`. This is often done for better editor autocompletion and type checking within style definitions. ```typescript // 34. Imitating StyleSheet.create() // Just to leverage VSCode's hints ``` -------------------------------- ### Map vs. forEach in JavaScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Compares the `map` and `forEach` array methods in JavaScript, detailing their differences in return values and behavior. ```javascript // 37. Difference between Map and ForEach in JS ``` -------------------------------- ### JavaScript History Overview Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Provides a high-level overview of the history of JavaScript, touching upon its evolution and key milestones. ```javascript // JS History Overview ``` -------------------------------- ### Type Aliases and Interfaces in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Provides a detailed comparison of `type` aliases and `interface` declarations in TypeScript, covering their syntax, capabilities, and recommended usage scenarios. ```typescript // 1. type // 2. Magical Usage of interface and Type (Usage not available in Kotlin and Java) // 3. Summary: // Similarities: Both type and interface can specify types for objects. // Differences: // ● Interfaces can only specify types for objects. They can extend. (I found that type can also extend). // ● Type aliases can not only specify types for objects but can actually alias any type. // Interface came first, then type. It is recommended to use type. ``` -------------------------------- ### Tuple vs. Array in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Compares and contrasts tuples and arrays in TypeScript, highlighting their differences in terms of fixed size and type constraints. ```typescript // 32. Difference between tuple and array // Array // Tuple ``` -------------------------------- ### Map vs. forEach in JavaScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Compares and contrasts the `map` and `forEach` array methods in JavaScript. Key differences include `map` returning a new array and supporting chaining, while `forEach` returns `undefined` and does not create a new array. ```javascript // 37. Difference between Map and ForEach in JS // 1. map is faster than forEach // 2. map returns a new array, does not affect the original array, foreach does not return a new array, forEach returns undefined // 3. map can be chained because it returns an array, forEach cannot // 4. map can use return (what is returned, it is equivalent to changing this item in the array (it does not affect the original array, it is equivalent to cloning the original array and changing the corresponding item in the cloned array), while return in forEach has no effect. forEach cannot use break, it will directly report an error. ``` -------------------------------- ### Describing Objects with Type Signatures and Record in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Shows how to describe object shapes using type signatures and the `Record` utility type in TypeScript. `Record` creates a type by mapping from K (a union of key types) to T (a mapped type). ```typescript // 8. TS Complete Explanation: Describing Objects with Type Signatures and Record // 9. Record ``` -------------------------------- ### RootStackParamList in Navigation Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Explains the concept of `RootStackParamList` likely used in navigation libraries (like React Navigation) for defining the parameters that can be passed to different screens in a stack navigator. ```typescript // 36. RootStackParamList ``` -------------------------------- ### Array Declaration and Tuple Definition (TypeScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc_%20%E3%80%8AJS%EF%BC%88TS%EF%BC%89%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E3%80%8B Illustrates two ways to declare arrays in TypeScript: using type[] and Array. It also shows how to define tuple types, which have fixed types and number of elements. Ensure type consistency when declaring arrays. ```typescript // Method 1: let variableName: Type[] = [value1, value2, value3] let arr1 : number[] = [1,2,3,4] console.log(arr1); // Method 2: let variableName: Array = [value1, value2, value3] let arr2: Array = [6,7,8] // Tuple type: let arr: [string, number, boolean] = [value1, value2, value3] let arr3 : [string, number, boolean] = ['东方不败', 100.123, true] console.log(arr3); ``` -------------------------------- ### Extending String Prototype (TypeScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc_%20%E3%80%8AJS%EF%BC%88TS%EF%BC%89%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E3%80%8B Demonstrates how to extend the built-in String prototype in TypeScript by declaring the new method in its interfaces. This allows adding custom methods to strings. Note: Modifying built-in prototypes can have side effects. ```typescript declare global { interface String { toCapital(): string; } } String.prototype.toCapital = function() { return this.charAt(0).toUpperCase() + this.slice(1); }; const greeting = "hello"; console.log(greeting.toCapital()); // "Hello" ``` -------------------------------- ### TypeScript Array and Tuple Type Definitions Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=en-us&singleDoc= Illustrates the distinct ways to define arrays and tuples in TypeScript. Arrays allow for homogeneous types, while tuples enforce a specific type and order for a fixed number of elements. ```typescript // Array Definition Method 1 let arr1 : number[] = [1,2,3,4]; console.log(arr1); // Array Definition Method 2 let arr2: Array = [6,7,8]; /* Note: After an array is defined, the data types within the array must be consistent with the type defined at the time of array creation, otherwise, there will be an error prompt, and in some cases, compilation may not pass. */ // Tuple Definition // In TypeScript, a tuple type specifies the types and number of elements in an array from the beginning. let arr3 : [string,number,boolean] = ['东方不败',100.123,true]; console.log(arr3); ``` -------------------------------- ### Array Declaration and Tuple Type (TypeScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc= Illustrates two ways to declare arrays in TypeScript: using `type[]` and `Array`. It also shows how to define a tuple type, which is an array with a fixed number of elements whose types are known. ```typescript // Method 1: let variableName : dataType[] = [value1, value2, value3] let arr1 : number[] = [1,2,3,4] console.log(arr1); // Output: [1,2,3,4] // Method 2: let variableName : Array = [value1, value2, value3] let arr2:Array=[6,7,8] /* Note: After an array is defined, the data type of the elements within the array must be consistent with the type defined when the array was declared. Otherwise, an error prompt will appear, and in some cases, compilation will not pass. */ // Tuple // In TypeScript, a tuple type is defined when the type and the number of elements in an array are already fixed. // Tuple type syntax: let arr : [ string ,number,boolean ] = [ 'Dauntless', 100 , true ] let arr3 : [string,number,boolean] = ['东方不败',100.123,true] console.log(arr3); // Output: ['东方不败',100.123,true] ``` -------------------------------- ### Indexed Access Types in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Demonstrates indexed access types in TypeScript, which allow you to look up a property in another type. This is useful for creating dynamic types based on existing object structures. ```typescript // 27. Indexed Access Types (Indexed Access Types) ``` -------------------------------- ### Understanding void 0 in JavaScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Explains the `void 0` expression in JavaScript and why it's sometimes preferred over `undefined`. ```javascript // 39. js cold knowledge void 0 what is it? why is it better than undefined?_Ao Jiao Wei Mei De Strawberry's Blog-CSDN Blog ``` -------------------------------- ### Interface vs. Type Alias (TypeScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc_%20%E3%80%8AJS%EF%BC%88TS%EF%BC%89%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E3%80%8B Compares TypeScript's `interface` and `type` keywords. Both can define object shapes, but `interface` is primarily for objects and can be extended, while `type` can alias any type and offers more flexibility. It's recommended to use `type`. ```typescript // Interface example interface Person { name: string; } interface Employee extends Person { employeeId: number; } // Type alias example type Point = { x: number; y: number }; type ID = number | string; let person: Person = { name: 'John' }; let employee: Employee = { name: 'Jane', employeeId: 123 }; let point: Point = { x: 10, y: 20 }; let userId: ID = 'abc'; ``` -------------------------------- ### Array vs. ForEach in JavaScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc_%20%E3%80%8AJS%EF%BC%88TS%EF%BC%89%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E3%80%8B Compares the `map` and `forEach` array methods in JavaScript. `map` returns a new array and supports chaining, while `forEach` iterates and returns `undefined`, without affecting the original array. `map` is generally faster. ```javascript const numbers = [1, 2, 3]; // Using map const doubled = numbers.map(num => num * 2); console.log(doubled); // [2, 4, 6] console.log(numbers); // [1, 2, 3] (original array unchanged) // Using forEach let sum = 0; numbers.forEach(num => { sum += num; }); console.log(sum); // 6 console.log(numbers); // [1, 2, 3] (original array unchanged) ``` -------------------------------- ### Declaring Tuples in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Illustrates how to declare tuples in TypeScript, which are arrays with a fixed number of elements and predetermined types. This ensures that each element in the tuple has a specific type. ```typescript // In TS, a tuple type is defined when the type and number of elements in an array are pre-determined. // Tuple type syntax: let arr : [ string ,number,boolean ] = [ ‘Dai Gōmō’ , 100 , true ] let arr3 : [string,number,boolean] = ['东方不败',100.123,true] console.log(arr3); // Output: ['东方不败',100.123,true] ``` -------------------------------- ### Record Utility Type (TypeScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc_%20%E3%80%8AJS%EF%BC%88TS%EF%BC%89%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E3%80%8B Explains TypeScript's `Record` utility type, which constructs an object type whose property keys are of type `K` and whose property values are of type `T`. Useful for creating dictionaries or maps. ```typescript type PageInfo = { link: string; title: string; }; type Page = Record<"about" | "contact" | "home", PageInfo>; const pages: Page = { about: { link: "/about", title: "About Us" }, contact: { link: "/contact", title: "Contact Page" }, home: { link: "/", title: "Homepage" } }; ``` -------------------------------- ### Union Types in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Explains and demonstrates the concept of union types in TypeScript. Union types allow a variable to hold values of one of several specified types, indicated by the `|` operator. ```typescript // 10. Union Types ``` -------------------------------- ### Generic Type as Call Signature in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Demonstrates how a generic type can be represented as a call signature of an object literal type, leading to the creation of generic interfaces. ```typescript // 25. We can also write the generic type as a call signature of an object literal type: // Which leads us to writing our first generic interface. Let’s take the object literal from the previous example and move it to an interface: ``` -------------------------------- ### Generics in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Explains the concept of generics in TypeScript, which allow for writing reusable code components that can work over a variety of types rather than a single one. Generic functions and classes enhance flexibility and type safety. ```typescript // 14. Generics // 4. Generics Usage ``` -------------------------------- ### Assign Generic Function to Generic Interface (TypeScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc= Demonstrates how to assign a generic function to a generic interface in TypeScript. This is useful for creating reusable function types that can work with different data types. ```typescript interface GenericIdentityFn { (arg: Type): Type; } function identity(arg: Type): Type { return arg; } let myIdentity: GenericIdentityFn = identity; ``` -------------------------------- ### Type Inference (TypeScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc_%20%E3%80%8AJS%EF%BC%88TS%EF%BC%89%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E3%80%8B Demonstrates TypeScript's type inference capabilities, where the compiler automatically deduces the type of a variable based on its initialization. This reduces the need for explicit type annotations in many cases. ```typescript let message = "Hello"; // TypeScript infers 'message' is of type string let count = 10; // TypeScript infers 'count' is of type number let isActive = true; // TypeScript infers 'isActive' is of type boolean ``` -------------------------------- ### Declaring Arrays in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Shows two common ways to declare arrays in TypeScript: using the type annotation `number[]` and using the generic `Array` syntax. It highlights the importance of type consistency within arrays. ```typescript // Method 1 let arr1 : number[] = [1,2,3,4] console.log(arr1); // Output: [1,2,3,4] // Method 2 let arr2:Array=[6,7,8] /* Note: After defining an array, the data type within the array must be consistent with the type defined when the array was declared, otherwise there will be an error prompt, and in some cases, compilation will not pass. */ ``` -------------------------------- ### Using 'keyof' Operator in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Illustrates the `keyof` operator in TypeScript, which is used to extract the union of property names (keys) of a given type. This is powerful for creating generic functions that operate on object keys. ```typescript // 7. keyof ``` -------------------------------- ### Inferring Return Type from Function Type in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Shows how to infer the return type from a function type in TypeScript. This is a common pattern in type utilities for extracting type information. ```typescript // 23. Extract return type from function type ``` -------------------------------- ### Spread and Rest Operators in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Explains the usage of the spread (`...`) and rest operators in TypeScript. The spread operator expands iterables or objects, while the rest operator collects multiple arguments into an array. ```typescript // 30. ...Spread, Rest ``` -------------------------------- ### Extracting Return Type from a Function Type in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Shows how to extract the return type from a function type in TypeScript. This is a common utility for creating more advanced type manipulations. ```typescript // Example demonstrating extracting return type (actual code not provided in source) ``` -------------------------------- ### Object.assign() in JavaScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Explains the functionality of `Object.assign()`, a method that copies all enumerable own properties from one or more source objects to a target object. It's commonly used for merging objects or creating shallow copies. ```javascript // Object.assign ``` -------------------------------- ### Destructuring in Function Declarations (TypeScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc_%20%E3%80%8AJS%EF%BC%88TS%EF%BC%89%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E3%80%8B Shows how to use destructuring in function parameters in TypeScript. This allows for concise access to object properties or array elements passed as arguments. It supports both object and array destructuring. ```typescript type C = { a: string, b: number } function f({ a, b }: C): void { // ... } function f([first, second]: [number, number]) { console.log(first); console.log(second); } f(input); ``` -------------------------------- ### True Falsy Value Narrowing in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Explains the concept of truthiness and falsiness narrowing in TypeScript. This involves the compiler understanding that a value is either truthy or falsy within a specific scope, allowing for type refinement. ```typescript // 4. Truthiness Narrowing ``` -------------------------------- ### Mapped Types in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Details mapped types in TypeScript, a feature for creating new types based on existing ones by mapping their properties. ```typescript // 28. Mapped Types (Mapped Types) ``` -------------------------------- ### Mapped Types in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Explains mapped types, a powerful feature in TypeScript for transforming existing types into new ones by iterating over their properties. ```typescript // 17. Mapped Types ``` -------------------------------- ### Mapped Types in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Explains mapped types in TypeScript, a feature that allows creating new types by transforming properties of existing types. This is fundamental for creating utility types like `Partial`, `Readonly`, and `Pick`. ```typescript // 17. Mapped Types // 28. Mapped Types (Mapped Types) ``` -------------------------------- ### Type Assertions in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Covers the use of type assertions in TypeScript, which allow developers to explicitly tell the compiler the type of a value when the compiler cannot infer it correctly. This should be used judiciously. ```typescript // 5. Type Judgment // 1. in ``` -------------------------------- ### Difference Between '===' and '==' in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Explains the difference between the strict equality operator (`===`) and the abstract equality operator (`==`) in TypeScript, a concept inherited from JavaScript. Strict equality checks both value and type, while abstract equality performs type coercion before comparison. ```typescript // 6. Difference between triple equals and double equals in TypeScript ``` -------------------------------- ### Type Predicates in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Introduces type predicates in TypeScript. These are special types of functions that return a boolean and assert that a variable is of a certain type within a certain scope, often used in conjunction with type guards. ```typescript // 12. Type Predicates ``` -------------------------------- ### Inference in Conditional Types in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Details the use of the `infer` keyword within conditional types in TypeScript. This allows for inferring types from within a conditional branch, making type manipulation more flexible. ```typescript // We find ourselves using conditional types to apply constraints and then extract types. This eventually becomes a very common operation, and conditional types make it easier. // For example, we can infer the element type in Flatten, rather than manually extracting it using indexed access types: ``` -------------------------------- ### Type Guarding with 'in' Operator in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Demonstrates the use of the `in` operator as a type guard in TypeScript. This operator checks if a property exists on an object, helping to narrow down the type within conditional blocks. ```typescript // 1. in ``` -------------------------------- ### Template Literal Types in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Introduces template literal types in TypeScript, which allow creating string literal types based on template literals. This enables powerful string manipulation and type checking at the type level. ```typescript // 29. Template Literal Types (Template Literal Types) ``` -------------------------------- ### Conditional Types (TypeScript) Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_singleDoc_%20%E3%80%8AJS%EF%BC%88TS%EF%BC%89%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E3%80%8B Explains TypeScript's conditional types, which resemble JavaScript's ternary operator. They allow for creating types that depend on other types. The `infer` keyword can be used within conditional types to deduce types. ```typescript // Example demonstrating conditional types and infer type Flatten = T extends Array ? U : T; type MyArray = number[]; type ElementType = Flatten; // ElementType will be number ``` -------------------------------- ### Type Gymnastics in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Refers to advanced type manipulation techniques in TypeScript, often involving complex combinations of utility types, conditional types, and mapped types to achieve sophisticated type-level programming. ```typescript // 13. Type Gymnastics ``` -------------------------------- ### Type Inference in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Covers type inference in TypeScript, where the compiler automatically determines the type of a variable or expression based on its value or usage. This reduces the need for explicit type annotations. ```typescript // 11. Type Inference ``` -------------------------------- ### Distributive Conditional Types in TypeScript Source: https://www.yuque.com/try402025/oq9euk/owdai3lb6fc903sv_language=zh-cn&singleDoc= Explains distributive conditional types in TypeScript. When a conditional type operates on a generic type parameter that is not constrained, the conditional type is distributed over each member of the union in that type parameter. ```typescript // 24. Distributive Conditional Types ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.