O'Reilly logo

Cross-platform Desktop Application Development: Electron, Node, NW.js, and React by Dmitry Sheiko

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generic type

The types we have just examined refer to a concrete type combination. In addition, TypeScript supports a so-called generic type that helps reusing the once created interface in different contexts. For example, if we want an interface for a data map, we can make it like this:

interface NumberDataMap { 
  [ key: string ]: number; 
} 

But this NumberDataMap accepts only numbers for the member values. Let's say, for string values, we have to create a new interface, such as StringDataMap. Alternatively, we can declare a generic DataMap that sets an arbitrary value type constraint when referred:

 interface DataMap<T> { [ key: string ]: T; } const numberMap: DataMap<number> = { foo: 1, bar: 2 }, stringMap: DataMap<string> = { foo: "foo", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required