Inline types

Custom types can also be defined inline.

Here is an example of a variable type:

const plane: {    name: string,    description: string} = {    name: "Plane",    description: "Something that flies"};

In this example, we have defined a custom shape. Any value assigned to the plane variable must have a name property of the string type, as well as a description property of the string type.

And here's another one for a function argument:

function foo(bar: { firstName: string, lastName: string}): void {    console.log(`Hello ${bar.firstName}.. or should I call you Mr      ${bar.lastName}?`);}foo({    firstName: "Sebastien",    lastName: "Dubois"});

In this last example, we defined a foo function requiring a single bar argument with another custom inline ...

Get Learn TypeScript 3 by Building Web Applications now with O’Reilly online learning.

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