Function return value

The value expected from a function when it is executed can also be strictly typed:

function stringIndex(char: string, index: number): string { const arr = char.split(' ') return arr[number];}

As you can see from the preceding code snippet, the return type comes after the brackets containing the parameters and before the opening curly bracket of the function's body. The preceding function is expected to, and must, return a string. Anything other than a string will scream at you with errors.

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.