Function parameters

You can tell TypeScript what type of values a function should expect, and it will strictly adhere to it. The following example shows a function that receives a typed string and a number as its parameters:

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

The char and index parameters have the string and number types, respectively. Even before things get to the browser, TypeScript will alert you when you try something silly:

function stringIndex(char: string, index: number) { const arr = char.split(' ') return arr[number];}stringIndex(true, 'silly') // Types don't match

Of course, function expressions are not left out:

const stringIndex = function (char: ...

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.