Created
August 15, 2019 04:41
-
-
Save AliN11/da8607634b4e537cc5e73dd5bfb10a3d to your computer and use it in GitHub Desktop.
Typescript Generics in classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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