plastikaweb
2/5/2019 - 9:05 AM

typescript generics

interface GenericIdentityFn<T> {
    (arg: T): T;
}

function identity<T>(arg: T): T {
    return arg;
}

const myIdentityStr: GenericIdentityFn<string> = identity;
const myIdentityNmb: GenericIdentityFn<number> = identity;

let ouput = myIdentityStr('string');
let ouput2 = myIdentityNmb(4);
class GenericNumber<T> {
    value: T;
    add: (x: T, y: T) => T;
}

const myG = new GenericNumber<number>();

myG.value = 0;
myG.add(4, 5);