Creating TypeScript types
The last section showed that TypeScript has a great set of standard types. In this section, we will learn how to create our own types. We will start by learning three different methods for creating object types. We will then learn about strongly-typing JavaScript classes. Lastly, we will learn two different methods for creating types for variables that hold a range of values.
Using object types
Objects are very common in JavaScript programs, so learning how to represent them in TypeScript is really important. In fact, we have already used an object type earlier in this chapter for the product
parameter in the calculateTotalPrice
function. Here is a reminder of the product
parameter’s type annotation:
function calculateTotalPrice( product: { name: string; unitPrice: number }, ... ) { ... }
An object type in TypeScript is represented a bit like a JavaScript object literal. However, instead of property values...