Skip to content

Instantly share code, notes, and snippets.

@AliN11
Created August 15, 2019 04:41
Show Gist options
  • Save AliN11/da8607634b4e537cc5e73dd5bfb10a3d to your computer and use it in GitHub Desktop.
Save AliN11/da8607634b4e537cc5e73dd5bfb10a3d to your computer and use it in GitHub Desktop.
Typescript Generics in classes
class Queue<T> {
private data: T[] = [];
push(item: T) {
this.data.push(item);
}
pop(): T | undefined {
return this.data.shift();
}
}
let obj = new Queue<number>();
obj.push(1);
obj.push("2"); Error: Argument of type '"2"' is not assignable to parameter of type 'number'
obj.push(3);
console.log(obj.pop());
console.log(obj.pop());
console.log(obj.pop());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment