typescript generic T code example
Example 1: typescript generic function
function firstElement<Type>(arr: Type[]): Type {
return arr[0];
}
const s = firstElement(["a", "b", "c"]);
const n = firstElement([1, 2, 3]);
Example 2: typescript generic type interface
interface IProcessor<T>
{
result:T;
process(a: T, b: T) => T;
}
Example 3: typescript generic type
function identity<T>(arg: T): T {
return arg;
}Try
Example 4: how to make a generic variable in javascript
var x = 1;
var y = 1;
if (x == y) {
x = x + 1
}