Type Annotations

Learn how to add type annotations to your code

Adding type annotations is an important part of your interaction with Flow.

Flow has a powerful ability to infer the types of your programs. The majority of your code can rely on it. Still, there are places where you’ll want to add types.

Imagine the following concat function for concatenating two strings together.

1
2
3
function concat(a, b) {
  return a + b;
}

When you use this function, Flow knows exactly what is going on.

1
concat("A", "B"); // Works!

However, you can use the + operator on strings or numbers, so this would also be valid.

1
concat(1, 2); // Works!

But supposed you only want to allow strings in your function. For that you can add types.

1
2
3
function concat(a: string, b: string) {
  return a + b;
}

Now you’ll get a warning from Flow if you try to use numbers.

1
2
3
4
5
6
7
// @flow
function concat(a: string, b: string) {
  return a + b;
}

concat("A", "B"); // Works!
concat(1, 2); // Error!
number This type is incompatible with the expected param type of string number This type is incompatible with the expected param type of string

Setting up “boundaries” with your types means you can tell Flow your intent on top of the inference it already does.

This guide will teach you the syntax and semantics of all the different types you can have in Flow.