Flow: Arrow functions cannot be typed

Created on 1 Mar 2015  路  6Comments  路  Source: facebook/flow

It doesn't appear to work (for TypeScript v1.4 is ok instead):

/* @flow */
var f = (x: number) => x * 2; // Flow Error: "Invalid left-hand side in formals list"
var f = (x: number): number => x * 2; // Flow Error: "Unexpected token :"

FWIW, this does work:

/* @flow */
var f: (x: number) => number = x => x * 2; // ok

% flow --version
Flow, a static type checker for JavaScript, version 0.4.0

incompleteness

Most helpful comment

Everything in @gcanti's original post should work now. Arrow functions that surround their parameters with parenthesis put parameter type annotations after the parameters and return type annotations after the parens. Type parameters go before the parens, like var identity = <T>(x: T): T => x.

We disallow x: number => x * 10 since it's ambiguous if the annotation is for the parameter type or return type.

All 6 comments

This is a duplicate of #37

Hi @gcanti - per @samwgoldman this is on the todo list. Thanks!

This has been fixed since 0.12. Thanks for the report!

Just a quick question - how exactly should I type the parameters and the returning type of the arrow function? As in the @gcanti original example?

Everything in @gcanti's original post should work now. Arrow functions that surround their parameters with parenthesis put parameter type annotations after the parameters and return type annotations after the parens. Type parameters go before the parens, like var identity = <T>(x: T): T => x.

We disallow x: number => x * 10 since it's ambiguous if the annotation is for the parameter type or return type.

Thanks! I couldn't find it anywhere.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidpelaez picture davidpelaez  路  3Comments

mmollaverdi picture mmollaverdi  路  3Comments

cubika picture cubika  路  3Comments

jamiebuilds picture jamiebuilds  路  3Comments

funtaps picture funtaps  路  3Comments