How to declare classes with interfaces using TypeScript

Interfaces let you specify behavior without specifying implementation; classes let you encapsulate implementation details behind an interface. TypeScript classes can encapsulate fields or methods, just as classes in other languages.

How to do it…

Here's an example of our Record structure, this time as a class with an interface:

class RecordInterface { call: string; lat: number; lng: number; constructor(c: string, la: number, lo: number) {} printLocation() {} } class Record implements RecordInterface { call: string; lat: number; lng: number; constructor(c: string, la: number, lo: number) { this.call = c; this.lat = la; this.lng = lo; } printLocation() { console.log(this.call + ': ' + this.lat ...

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.