Interfaces for JavaScript object types

We agree that the following is a valid JavaScript object:

// Option baglet options = {show: true, container: '#main'};

It is a valid JavaScript code but loosely typed. All this while, we have been discussing strings, numbers, Boolean, and even arrays. We are yet to consider objects.

As you may have imagined, the following code snippet demonstrates a typed version of the preceding example:

// Typed objectlet options: {show: boolean, container: string};// Assing valuesoptions = {show: true, container: '#main'};

This is correct in fact, but TypeScript could use interfaces to make it more maintainable and easy to comprehend. The following is how we write interfaces in TypeScript:

interface OptionBag { show: ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.