-
Notifications
You must be signed in to change notification settings - Fork 0
Generics
Jorge Castro edited this page Nov 10, 2025
·
1 revision
function loggingIdentity<T>(table: T[]): string {
return `Total elements in the table: ${table.length}`;
}type Product = { id: number; name: string; price: number };
function getTotal<T extends Product>(products: T[]): string {
const totalPrice = products.reduce((sum, p) => sum + p.price, 0);
return `There are ${products.length} products. And the total price is ${totalPrice}.`;
}type ApiResponse<T = string> = { data: T; success: boolean; };
const response1: ApiResponse = { data: "OK", success: true };
const response2: ApiResponse<number> = { data: 200, success: true };- Sync-Obsidian-with-GitHub-on-iOS
- Obsidian-community-plugins
- Optimizing-Novel-Word-Count-Plugin-for-Git-Users
- What is TypeScript, and why you should use it
- Quick setup
- Inference and type annotations
- Fundamental Types
- Object Types
- Union Types
- Type Aliases and Interfaces
- Type Assertions
- Literal Types
- null and undefined
- Narrowing
- Creating Types from Types
- Common Built‐in and Utility Types
- tsc CLI ‐ (The Compiler)
- Avoid using Object or {}
- Ignoring TypeScript errors
- TypeScript resources