Functions and function types

JavaScript functions are loosely typed and are some of the most common sources of errors in the language. This is what a basic function looks like:

function stringToArray(char) {  return char.split(' ')}

How sure are we that char is not a number? Well, we might not have any control over what the developer consuming stringToArray will pass in. This is why we need to be strict about the value types using TypeScript.

Functions use types in two different parts of their declaration:

  1. Function parameters
  2. Function return value

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.