flow icon indicating copy to clipboard operation
flow copied to clipboard

Arrow functions cannot be typed

Open gcanti opened this issue 11 years ago • 6 comments

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

gcanti avatar Mar 01 '15 10:03 gcanti

This is a duplicate of #37

samwgoldman avatar Mar 01 '15 19:03 samwgoldman

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

bhosmer avatar Mar 03 '15 02:03 bhosmer

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

samwgoldman avatar Jun 24 '15 00:06 samwgoldman

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?

karelbilek avatar Dec 14 '15 11:12 karelbilek

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.

gabelevi avatar Dec 14 '15 14:12 gabelevi

Thanks! I couldn't find it anywhere.

karelbilek avatar Dec 14 '15 14:12 karelbilek