generics default typescrpt code example
Example 1: extend generics in functions typescript
interface Lengthwise {
length: number;
}
function loggingIdentity<T extends Lengthwise>(arg: T): T {
console.log(arg.length);
return arg;
}
Example 2: typescript generic class
class Person<T, U, C> {
name: T
age: U
hobby: C[]
constructor(name: T, age: U, hobby: C[]) {
this.name = name
this.age = age
this.hobby = hobby
}
}
const output = new Person<string, number, string>('john doe', 23, ['swimming', 'traveling', 'badminton'])
console.log(output)