Type Annotations
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
For example, you don't have to produce annotations for common patterns like Array.map
:
1["foo", "bar"].map(s => ( // s is inferred to have type string2 s.length3));
Still, there are places where you'll want to add types.
Imagine the following concat
function for concatenating two strings together.
1function concat(a, b) { 2 return a + b;3}
1:17-1:17: Missing an annotation on `a`. [missing-local-annot]1:20-1:20: Missing an annotation on `b`. [missing-local-annot]
You need to add annotations on parameters of concat
, so that Flow can type
check its body. Now you'll get a warning from Flow if you are calling this
function with unexpected types.
1function concat(a: string, b: string) {2 return a + b;3}4
5concat("A", "B"); // Works!6concat(1, 2); // Error!
6:8-6:8: Cannot call `concat` with `1` bound to `a` because number [1] is incompatible with string [2]. [incompatible-call]6:11-6:11: Cannot call `concat` with `2` bound to `b` because number [1] is incompatible with string [2]. [incompatible-call]
This guide will teach you the syntax and semantics of all the different types you can have in Flow.