4 category: JavaScript libraries
18 function square (n: number) {
22 const four = square(2)
26 Most of what you need to do is to simply add annotations to function arguments!
28 See: [flow.org docs](https://flow.org/en/docs/)
33 function square (n: number) {
39 `result` is inferred to be a number because `number * number` will result in a number. There's no need to give it annotations.
51 {: data-line="1,2,3,4,5,6"}
54 function greet(user: Person) {
55 console.log('hello', user.name)
61 greet({ name: 'Miles Davis', ··· })
64 This is the typical way to define the shape of complex objects.
69 const count: number = 200
72 You typically don't need to do this, function args are often enough.
74 See: [Variable types](https://flow.org/en/docs/types/variables/)
76 ### Importing and exporting
79 import type { Person } from './types'
83 export type Person = {
88 See: [Module types](https://flow.org/en/docs/types/modules)
93 type Action = number | string
97 type Direction = 'left' | 'right'
100 See: [Unions](https://flow.org/en/docs/types/unions/)
114 const a: Album = { } // ✗ Error
115 const a: Album = { name: 'Blue' } // ✓ OK
116 const a: Album = { name: null } // ✓ OK
117 const a: Album = { name: undefined } // ✓ OK
120 This makes `name` either a string or null.
122 See: [Maybe types](https://flow.org/en/docs/types/primitives/#toc-maybe-types)
124 ### Optional properties
134 const a: Album = { } // ✓ OK
135 a.name = 'Blue' // ✓ OK
136 a.name = null // ✗ Error
137 a.name = undefined // ✓ OK
140 This makes an `Album` valid even if `name` is not part of the keys. This is different from "maybe" types.
142 See: [Optional properties](https://flow.org/en/docs/types/primitives/#toc-optional-object-properties)
147 ### Extra object fields
162 a.genre = 'House' // ✓ OK
166 You can add more fields to an object.
168 See: [Width subtyping](https://flow.org/en/docs/lang/width-subtyping/)
170 ### Exact object types
181 const a: Artist = { ··· }
182 a.genre = 'House' // ✗ Error
186 Exact object types prevent extra properties from being added to an object.
188 See: [Exact object types](https://flow.org/en/docs/types/objects/#toc-exact-object-types)
199 See: [Dynamic object keys](https://flow.org/docs/objects.html#objects-as-maps)
205 | Type | Description |
212 | `void` | undefined |
213 | `null` | null (but not undefined) |
215 | `{a: Number}` | Object with a shape |
216 | `[any, number]` | Tuples (fixed-length arrays) |
223 | `?number` | Maybe (number, void, null) |
224 | `a | b` | Union types |
229 type Suit = "Diamonds" | "Clubs" | "Hearts" | "Spades"
237 type Country = $Keys<typeof countries>
240 See: [Enums](https://flow.org/docs/utility-types.html#keyst)
248 qux: (foo: string, bar: number) => boolean
256 See: [Type aliases](https://flow.org/docs/quick-reference.html#type-aliases)
261 class GenericClass<T> {
263 constructor (x: T) { ... }
266 var n: GenericClass<number> = new GenericClass(0)
269 See: [Generic classes](https://flow.org/docs/quick-reference.html#generics)
279 toJSON() { return '{}' }
285 See: [Interfaces](https://flow.org/docs/quick-reference.html#interfaces)
290 const callback: () => void = function () {}
296 callback: (item: T) => boolean
302 See: [Functions](https://flow.org/docs/functions.html)
307 import type { Person } from '../person'
308 import typeof Config from '../config'
312 export type Person = { id: string }
319 export type Foo = { ... }
322 function add(n /*: number */) { ... }
336 class Foo extends React.Component<Props, State> {
346 var myNumbers: Array<number> = [42]
347 function foo(): any { return 42 }
348 var b: boolean = false
349 var b: ?boolean = false /* maybe */
350 var b: string | boolean = false
352 var a: Class<MyClass> = MyClass
353 var b: MyClass = new a()
356 ### Function signature
359 type Callback = (?Error, string) => any
361 function fetch (callback: Callback) {
368 - [Flow website](https://www.saltycrane.com/flow-type-cheat-sheet/latest/) _(flow.org)_
369 - [Getting started with Flow](https://flow.org/en/docs/getting-started/) _(flow.org)_
370 - [Flow type cheatsheet](https://www.saltycrane.com/flow-type-cheat-sheet/latest/) _(saltycrane.com)_