Questions tagged [flowtype]

Flow is a static type checker, designed to find type errors in JavaScript programs, created by Facebook.

What is Flow?

Flow is a static type checker, designed to find type errors in JavaScript programs.

The goal of Flow is to find errors in JavaScript code with little programmer effort. Flow relies heavily on type inference to find type errors even when the program has not been annotated - it precisely tracks the types of variables as they flow through the program.

At the same time, Flow is a gradual type system. Any parts of your program that are dynamic in nature can easily bypass the type checker, so you can mix statically typed code with dynamic code.

Flow also supports a highly expressive type language. Flow types can express much more fine-grained distinctions than traditional type systems. For example, Flow helps you catch errors involving null, unlike most type systems.

2301 questions
27
votes
1 answer

What does flow {| brace pipe |} syntax do?

What does this Flow syntax mean/do? type Options = {| assumeValid?: boolean, commentDescriptions?: boolean, |}; I can't seem to find where the {| ... |} syntax is documented.
mpen
  • 237,624
  • 230
  • 766
  • 1,119
27
votes
2 answers

flow type question mark before or after param?

Can someone explain the difference between: function foo(bar: ?string) { console.log(bar); } and: function foo(bar?: string) { console.log(bar); } When to use one over the other?
Vic
  • 6,156
  • 3
  • 36
  • 49
27
votes
2 answers

Using Promise as return type in Flow

I'm using Flow 0.30.0. I have a function that returns a promise: function process(callback:Function):Promise { return new Promise((r,re) => callback) } Flow raises an error, complaining: function process(callback:Function):Promise { …
Allyl Isocyanate
  • 12,435
  • 15
  • 71
  • 117
26
votes
2 answers

What is the use of asterisk (*) type in Flow and what is the equivalent of that in TypeScript?

First of all, I am mostly familiar with TypeScript. Flow looks very similar to TS in many ways, but I recently stumbled upon an asterisk (*) type. At first, I thought it was a synonym for "any", but now, after reading some of the release notes for…
NeverwinterMoon
  • 1,691
  • 15
  • 21
26
votes
2 answers

Dynamic property names in flow typed object

I'm slowly and surely working flowType into my code, but I'm struggling with one concept. How do I specify the type of unknown, dynamically named properties of an object? For example my user object might have an object containing organisations with…
Sam Matthews
  • 597
  • 1
  • 10
  • 25
25
votes
1 answer

How to type check a Date object in flow?

I have a function that takes a Date object as its paramater. I am not able to figure out how to annotate this. function diffDate (start: any, end:any) { //.... } How do I type annotate start and end as Date objects?
Aakash Sigdel
  • 7,704
  • 5
  • 29
  • 37
24
votes
2 answers

When do you use an interface over a type alias in flow?

The interface and type declarations seems to do the same thing. When do you use one over the other? type Fooable = { foo(): string } vs interface Fooable { foo(): string }
m0meni
  • 14,160
  • 14
  • 66
  • 120
23
votes
2 answers

typed react - props as `type` or an `interface`

I have react code export default class MyComponent extends Component The question is, do I write props like a type or an interface? type Props = { isActive: Boolean, onClick: Function } or interface Props { isActive:…
Ivan Hanák
  • 1,854
  • 1
  • 11
  • 23
23
votes
4 answers

StyleSheet.create "not covered by flow"

I am using flow 0.42.0 in a react native project with nuclide. With the default project, I get the following message: Along with the following errors: > flow check index.ios.js:40 40:
Daniel Centore
  • 2,968
  • 1
  • 15
  • 35
23
votes
3 answers

How to use/define Enums with Flow type checking?

I'm trying to migrate an existing codebase to use Flow. Since this project started without Flow, I'm using a pretty typical JS pattern for enums and such. Here are a few definitions I want to export const LOAN_STATUS = { PENDING: 'pending', …
Chet
  • 14,628
  • 14
  • 57
  • 101
22
votes
1 answer

How to write a flowtype for arrow function with generic type

How do I write flowtype for the following code? The function argument is an array of generic type. const fn = (array) => Promise.resolve(array[0]);
Joon
  • 7,348
  • 5
  • 41
  • 64
21
votes
2 answers

How to specify a Moment.js object annotation in Flow

I'm currently learning Flow by applying it to an existing project and looking to annotate function parameters as a Moment.JS objects. Using flow-typed I was able to install a library definition for Moment.JS which appears to have the type I'm…
clhenrick
  • 748
  • 6
  • 12
20
votes
2 answers

flow types with constant strings, and dependent types

Say I have the following constant string: export default const FOO = 'FOO' Say I import this in a flow annotated file like so: import FOO from '../consts/Foo' I then have a function: const example = (foo : string) : {| type: FOO, foo: string |} =>…
Abraham P
  • 13,493
  • 11
  • 50
  • 108
20
votes
4 answers

How to set a default value for a Flow type?

I have defined a custom Flow type export type MyType = { code: number, type: number = 1, } I want the type parameter to default as 1 if no value is present. However, Flow is complaining with Unexpected token =. Can this be done with…
amb
  • 4,591
  • 6
  • 35
  • 67
20
votes
2 answers

ESLint with Arbnb config and Facebook Flow together

I am using ESLint in a project and want to use Facebook flow as well, But I am getting warnings from ESLint on flow type annotations. I have .flowconfig and .eslintrc in project root. .eslintrc: // When you write javascript you should follow these…
Marcel Mandatory
  • 1,421
  • 11
  • 24