Skip to main content

Comment Types

Flow supports a comment-based syntax, which makes it possible to use Flow without having to compile your files.

1/*::2type MyAlias = {3  foo: number,4  bar: boolean,5  baz: string,6};7*/8
9function method(value /*: MyAlias */) /*: boolean */ {10  return value.bar;11}12
13method({foo: 1, bar: true, baz: ["oops"]});
13:33-13:40: Cannot call `method` with object literal bound to `value` because array literal [1] is incompatible with string [2] in property `baz`. [incompatible-call]

These comments allow Flow to work in plain JavaScript files without any additional work.

Comment types syntax

There are two primary pieces of the syntax: type includes and type annotations.

Type include comments

If you want to have Flow treat a comment as if it were normal syntax, you can do so by adding a double colon :: to the start of the comment:

1/*::2type MyAlias = {3  foo: number,4  bar: boolean,5  baz: string,6};7*/8
9class MyClass {10  /*:: prop: string; */11}

This includes the code into the syntax that Flow sees:

1type MyAlias = {2  foo: number,3  bar: boolean,4  baz: string,5};6
7class MyClass {8  prop: string;9}

But JavaScript ignores these comments, so your code is valid JavaScript syntax:

1class MyClass {2
3}

This syntax is also available in a flow-include form:

1/*flow-include2type MyAlias = {3  foo: number,4  bar: boolean,5  baz: string,6};7*/8
9class MyClass {10  /*flow-include prop: string; */11}

Type annotation comments

Instead of typing out a full include every time, you can also use the type annotation shorthand with a single colon : at the start of the comment.

1function method(param /*: string */) /*: number */ {2  return 1;3}

This would be the same as including a type annotation inside an include comment.

1function method(param /*:: : string */) /*:: : number */ {2  return 1;3}

Note: If you want to use optional function parameters you'll need to use the include comment form.


Special thanks to: Jarno Rantanen for building flotate and supporting us merging his syntax upstream into Flow.