Skip to Main Content
Advanced TypeScript Programming Projects
book

Advanced TypeScript Programming Projects

by Peter O'Hanlon
July 2019
Intermediate to advanced content levelIntermediate to advanced
416 pages
10h 6m
English
Packt Publishing
Content preview from Advanced TypeScript Programming Projects

Using the same code with different types and using generics

When we first start developing classes in TypeScript, it is very common for us to repeat the same code again and again, only changing the type that we are relying on. For instance, if we wanted to store a queue of integers, we might be tempted to write the following class:

class QueueOfInt {    private queue : number[]= [];    public Push(value : number) : void {        this.queue.push(value);    }    public Pop() : number | undefined {        return this.queue.shift();    }}

Calling this code is as easy as this:

const intQueue : QueueOfInt = new QueueOfInt();intQueue.Push(10);intQueue.Push(35);console.log(intQueue.Pop()); // Prints 10console.log(intQueue.Pop()); // Prints 35

Later on, we decide that we also ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learn TypeScript 3 by Building Web Applications

Learn TypeScript 3 by Building Web Applications

Sebastien Dubois, Alexis Georges

Publisher Resources

ISBN: 9781789133042Supplemental Content