Generics allow you to write a class or method that can work with any data type.
// Simple Generic
function echo(data: any) { return data; } console.log(echo("Max")); console.log(echo(27)); console.log(echo({name: "Max", age: 27})); // Better Generic
function betterEcho<T>(data: T) { return data; } console.log(betterEcho("Max").length); console.log(betterEcho<number>(27)); console.log(betterEcho({name: "Max", age: 27})); // Built-in Generics
const testResults: Array<number> = [1.94, 2.33]; testResults.push(-2.99); console.log(testResults); // Arrays
function printAll<T>(args: T[]) { args.forEach((element) => console.log(element)); } printAll<string>(["Apple", "Banana"]); // Generic Types
const echo2: <T>(data: T) => T = betterEcho; console.log(echo2<string>("Something")); // Generic Class
class SimpleMath<T extends number | string, U extends number | string> { baseValue: T; multiplyValue: U; calculate(): number { return +this.baseValue * +this.multiplyValue; } } const simpleMath = new SimpleMath<string, number>(); simpleMath.baseValue = "10"; simpleMath.multiplyValue = 20; console.log(simpleMath.calculate());
Comments
Post a Comment