Grokking TypeScript
Vote

0% completed

​

Generic Classes

How many classes does it take to store a number, a string and a user in the same way? With only the tools from the earlier chapters, the answer is three, and each one repeats the same code. A generic class reduces that to one class that takes a type parameter, in the same way a generic function does.

One Class for Each Type

Here is a small class that holds one number and lets you read or replace it:

class NumberStore { private value: number; constructor(value: number) { this.value = value; } getValue(): number { return this.value; }

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content