UNPKG

164 kBMarkdownView Raw
1<a name="eslint-plugin-flowtype"></a>
2# eslint-plugin-flowtype
3
4[![GitSpo Mentions](https://gitspo.com/badges/mentions/gajus/eslint-plugin-flowtype?style=flat-square)](https://gitspo.com/mentions/gajus/eslint-plugin-flowtype)
5[![NPM version](http://img.shields.io/npm/v/eslint-plugin-flowtype.svg?style=flat-square)](https://www.npmjs.org/package/eslint-plugin-flowtype)
6[![Travis build status](http://img.shields.io/travis/gajus/eslint-plugin-flowtype/master.svg?style=flat-square)](https://travis-ci.org/gajus/eslint-plugin-flowtype)
7[![js-canonical-style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
8
9[Flow type](http://flowtype.org/) linting rules for ESLint.
10
11* [eslint-plugin-flowtype](#eslint-plugin-flowtype)
12 * [Installation](#eslint-plugin-flowtype-installation)
13 * [Configuration](#eslint-plugin-flowtype-configuration)
14 * [Shareable configurations](#eslint-plugin-flowtype-configuration-shareable-configurations)
15 * [Community maintained configurations](#eslint-plugin-flowtype-configuration-community-maintained-configurations)
16 * [Settings](#eslint-plugin-flowtype-settings)
17 * [`onlyFilesWithFlowAnnotation`](#eslint-plugin-flowtype-settings-onlyfileswithflowannotation)
18 * [Rules](#eslint-plugin-flowtype-rules)
19 * [`array-style-complex-type`](#eslint-plugin-flowtype-rules-array-style-complex-type)
20 * [`array-style-simple-type`](#eslint-plugin-flowtype-rules-array-style-simple-type)
21 * [`arrow-parens`](#eslint-plugin-flowtype-rules-arrow-parens)
22 * [`boolean-style`](#eslint-plugin-flowtype-rules-boolean-style)
23 * [`define-flow-type`](#eslint-plugin-flowtype-rules-define-flow-type)
24 * [`delimiter-dangle`](#eslint-plugin-flowtype-rules-delimiter-dangle)
25 * [`generic-spacing`](#eslint-plugin-flowtype-rules-generic-spacing)
26 * [`newline-after-flow-annotation`](#eslint-plugin-flowtype-rules-newline-after-flow-annotation)
27 * [`no-dupe-keys`](#eslint-plugin-flowtype-rules-no-dupe-keys)
28 * [`no-existential-type`](#eslint-plugin-flowtype-rules-no-existential-type)
29 * [`no-flow-fix-me-comments`](#eslint-plugin-flowtype-rules-no-flow-fix-me-comments)
30 * [`no-internal-flow-type`](#eslint-plugin-flowtype-rules-no-internal-flow-type)
31 * [`no-mixed`](#eslint-plugin-flowtype-rules-no-mixed)
32 * [`no-mutable-array`](#eslint-plugin-flowtype-rules-no-mutable-array)
33 * [`no-primitive-constructor-types`](#eslint-plugin-flowtype-rules-no-primitive-constructor-types)
34 * [`no-types-missing-file-annotation`](#eslint-plugin-flowtype-rules-no-types-missing-file-annotation)
35 * [`no-unused-expressions`](#eslint-plugin-flowtype-rules-no-unused-expressions)
36 * [`no-weak-types`](#eslint-plugin-flowtype-rules-no-weak-types)
37 * [`object-type-curly-spacing`](#eslint-plugin-flowtype-rules-object-type-curly-spacing)
38 * [`object-type-delimiter`](#eslint-plugin-flowtype-rules-object-type-delimiter)
39 * [`require-compound-type-alias`](#eslint-plugin-flowtype-rules-require-compound-type-alias)
40 * [`require-exact-type`](#eslint-plugin-flowtype-rules-require-exact-type)
41 * [`require-indexer-name`](#eslint-plugin-flowtype-rules-require-indexer-name)
42 * [`require-inexact-type`](#eslint-plugin-flowtype-rules-require-inexact-type)
43 * [`require-parameter-type`](#eslint-plugin-flowtype-rules-require-parameter-type)
44 * [`require-readonly-react-props`](#eslint-plugin-flowtype-rules-require-readonly-react-props)
45 * [`require-return-type`](#eslint-plugin-flowtype-rules-require-return-type)
46 * [`require-types-at-top`](#eslint-plugin-flowtype-rules-require-types-at-top)
47 * [`require-valid-file-annotation`](#eslint-plugin-flowtype-rules-require-valid-file-annotation)
48 * [`require-variable-type`](#eslint-plugin-flowtype-rules-require-variable-type)
49 * [`semi`](#eslint-plugin-flowtype-rules-semi)
50 * [`sort-keys`](#eslint-plugin-flowtype-rules-sort-keys)
51 * [`space-after-type-colon`](#eslint-plugin-flowtype-rules-space-after-type-colon)
52 * [`space-before-generic-bracket`](#eslint-plugin-flowtype-rules-space-before-generic-bracket)
53 * [`space-before-type-colon`](#eslint-plugin-flowtype-rules-space-before-type-colon)
54 * [`spread-exact-type`](#eslint-plugin-flowtype-rules-spread-exact-type)
55 * [`type-id-match`](#eslint-plugin-flowtype-rules-type-id-match)
56 * [`type-import-style`](#eslint-plugin-flowtype-rules-type-import-style)
57 * [`union-intersection-spacing`](#eslint-plugin-flowtype-rules-union-intersection-spacing)
58 * [`use-flow-type`](#eslint-plugin-flowtype-rules-use-flow-type)
59 * [`use-read-only-spread`](#eslint-plugin-flowtype-rules-use-read-only-spread)
60 * [`valid-syntax`](#eslint-plugin-flowtype-rules-valid-syntax)
61
62
63<a name="eslint-plugin-flowtype-installation"></a>
64## Installation
65
661. Install [ESLint](https://www.github.com/eslint/eslint).
671. Install [`babel-eslint`](https://github.com/babel/babel-eslint) parser (ESLint parser [does not support type annotations](https://github.com/eslint/eslint/issues/2157)).
681. Install [`eslint-plugin-flowtype`](https://github.com/gajus/eslint-plugin-flowtype) plugin.
69
70<!-- -->
71
72```sh
73npm install eslint --save-dev
74npm install babel-eslint --save-dev
75npm install eslint-plugin-flowtype --save-dev
76
77# Or all at once:
78npm install eslint babel-eslint eslint-plugin-flowtype --save-dev
79```
80
81<a name="eslint-plugin-flowtype-configuration"></a>
82## Configuration
83
841. Set `parser` property to `babel-eslint`.
851. Add `plugins` section and specify `eslint-plugin-flowtype` as a plugin.
861. Enable rules.
87
88<!-- -->
89
90```json
91{
92 "parser": "babel-eslint",
93 "plugins": [
94 "flowtype"
95 ],
96 "rules": {
97 "flowtype/boolean-style": [
98 2,
99 "boolean"
100 ],
101 "flowtype/define-flow-type": 1,
102 "flowtype/delimiter-dangle": [
103 2,
104 "never"
105 ],
106 "flowtype/generic-spacing": [
107 2,
108 "never"
109 ],
110 "flowtype/no-mixed": 2,
111 "flowtype/no-primitive-constructor-types": 2,
112 "flowtype/no-types-missing-file-annotation": 2,
113 "flowtype/no-weak-types": 2,
114 "flowtype/object-type-delimiter": [
115 2,
116 "comma"
117 ],
118 "flowtype/require-parameter-type": 2,
119 "flowtype/require-readonly-react-props": 0,
120 "flowtype/require-return-type": [
121 2,
122 "always",
123 {
124 "annotateUndefined": "never"
125 }
126 ],
127 "flowtype/require-valid-file-annotation": 2,
128 "flowtype/semi": [
129 2,
130 "always"
131 ],
132 "flowtype/space-after-type-colon": [
133 2,
134 "always"
135 ],
136 "flowtype/space-before-generic-bracket": [
137 2,
138 "never"
139 ],
140 "flowtype/space-before-type-colon": [
141 2,
142 "never"
143 ],
144 "flowtype/type-id-match": [
145 2,
146 "^([A-Z][a-z0-9]+)+Type$"
147 ],
148 "flowtype/union-intersection-spacing": [
149 2,
150 "always"
151 ],
152 "flowtype/use-flow-type": 1,
153 "flowtype/valid-syntax": 1
154 },
155 "settings": {
156 "flowtype": {
157 "onlyFilesWithFlowAnnotation": false
158 }
159 }
160}
161```
162
163<a name="eslint-plugin-flowtype-configuration-shareable-configurations"></a>
164### Shareable configurations
165
166<a name="eslint-plugin-flowtype-configuration-shareable-configurations-recommended"></a>
167#### Recommended
168
169This plugin exports a [recommended configuration](./src/configs/recommended.json) that enforces Flow type good practices.
170
171To enable this configuration use the extends property in your `.eslintrc` config file:
172
173```json
174{
175 "extends": [
176 "plugin:flowtype/recommended"
177 ],
178 "plugins": [
179 "flowtype"
180 ]
181}
182```
183
184See [ESLint documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files) for more information about extending configuration files.
185
186<a name="eslint-plugin-flowtype-configuration-community-maintained-configurations"></a>
187### Community maintained configurations
188
189The following are third-party submitted/ maintained configurations of `eslint-plugin-flowtype`:
190
191* https://github.com/wemake-services/eslint-config-flowtype-essential
192
193<a name="eslint-plugin-flowtype-settings"></a>
194## Settings
195
196<a name="eslint-plugin-flowtype-settings-onlyfileswithflowannotation"></a>
197### <code>onlyFilesWithFlowAnnotation</code>
198
199When `true`, only checks files with a [`@flow` annotation](http://flowtype.org/docs/about-flow.html#gradual) in the first comment.
200
201```js
202{
203 "settings": {
204 "flowtype": {
205 "onlyFilesWithFlowAnnotation": true
206 }
207 }
208}
209```
210
211<a name="eslint-plugin-flowtype-rules"></a>
212## Rules
213
214<!-- Rules are sorted alphabetically. -->
215
216<a name="eslint-plugin-flowtype-rules-array-style-complex-type"></a>
217### <code>array-style-complex-type</code>
218
219_The `--fix` option on the command line automatically fixes problems reported by this rule._
220
221Enforces a particular annotation style of complex types.
222
223Type is considered complex in these cases:
224
225* [Maybe type](https://flow.org/en/docs/types/maybe/)
226* [Function type](https://flow.org/en/docs/types/functions/)
227* [Object type](https://flow.org/en/docs/types/objects/)
228* [Tuple type](https://flow.org/en/docs/types/tuples/)
229* [Union type](https://flow.org/en/docs/types/unions/)
230* [Intersection type](https://flow.org/en/docs/types/intersections/)
231
232This rule takes one argument.
233
234If it is `'verbose'` then a problem is raised when using `Type[]` instead of `Array<Type>`.
235
236If it is `'shorthand'` then a problem is raised when using `Array<Type>` instead of `Type[]`.
237
238The default value is `'verbose'`.
239
240The following patterns are considered problems:
241
242```js
243type X = (?string)[]
244// Message: Use "Array<?string>", not "(?string)[]"
245
246// Options: ["verbose"]
247type X = (?string)[]
248// Message: Use "Array<?string>", not "(?string)[]"
249
250// Options: ["shorthand"]
251type X = Array<?string>
252// Message: Use "(?string)[]", not "Array<?string>"
253
254// Options: ["shorthand"]
255type X = Array<{foo: string}>
256// Message: Use "{foo: string}[]", not "Array<{foo: string}>"
257
258type X = (string | number)[]
259// Message: Use "Array<string | number>", not "(string | number)[]"
260
261type X = (string & number)[]
262// Message: Use "Array<string & number>", not "(string & number)[]"
263
264type X = [string, number][]
265// Message: Use "Array<[string, number]>", not "[string, number][]"
266
267type X = {foo: string}[]
268// Message: Use "Array<{foo: string}>", not "{foo: string}[]"
269
270type X = (string => number)[]
271// Message: Use "Array<string => number>", not "(string => number)[]"
272
273type X = {
274 foo: string,
275 bar: number
276}[]
277// Message: Use "Array<{ foo: string, bar: number }>", not "{ foo: string, bar: number }[]"
278
279type X = {
280 foo: string,
281 bar: number,
282 quo: boolean,
283 hey: Date
284}[]
285// Message: Use "Array<Type>", not "Type[]"
286```
287
288The following patterns are not considered problems:
289
290```js
291type X = Array<?string>
292
293// Options: ["verbose"]
294type X = Array<?string>
295
296// Options: ["shorthand"]
297type X = (?string)[]
298
299// Options: ["shorthand"]
300type X = Array<string>
301
302// Options: ["shorthand"]
303// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
304type X = Array<?string>
305```
306
307
308
309<a name="eslint-plugin-flowtype-rules-array-style-simple-type"></a>
310### <code>array-style-simple-type</code>
311
312_The `--fix` option on the command line automatically fixes problems reported by this rule._
313
314Enforces a particular array type annotation style of simple types.
315
316Type is considered simple in these cases:
317
318* [Primitive types](https://flow.org/en/docs/types/primitives/)
319* [Literal types](https://flow.org/en/docs/types/literals/)
320* [Mixed type](https://flow.org/en/docs/types/mixed/)
321* [Any type](https://flow.org/en/docs/types/any/)
322* [Class type](https://flow.org/en/docs/types/classes/)
323* [Generic type](https://flow.org/en/docs/types/generics/)
324* Array type [shorthand notation](https://flow.org/en/docs/types/arrays/#toc-array-type-shorthand-syntax)
325
326This rule takes one argument.
327
328If it is `'verbose'` then a problem is raised when using `Type[]` instead of `Array<Type>`.
329
330If it is `'shorthand'` then a problem is raised when using `Array<Type>` instead of `Type[]`.
331
332The default value is `'verbose'`.
333
334The following patterns are considered problems:
335
336```js
337type X = string[]
338// Message: Use "Array<string>", not "string[]"
339
340// Options: ["verbose"]
341type X = string[]
342// Message: Use "Array<string>", not "string[]"
343
344// Options: ["shorthand"]
345type X = Array<string>
346// Message: Use "string[]", not "Array<string>"
347
348type X = Date[]
349// Message: Use "Array<Date>", not "Date[]"
350
351type X = Promise<string>[]
352// Message: Use "Array<Promise<string>>", not "Promise<string>[]"
353
354type X = $Keys<{foo: string}>[]
355// Message: Use "Array<$Keys<{foo: string}>>", not "$Keys<{foo: string}>[]"
356
357type X = any[]
358// Message: Use "Array<any>", not "any[]"
359
360type X = mixed[]
361// Message: Use "Array<mixed>", not "mixed[]"
362
363type X = void[]
364// Message: Use "Array<void>", not "void[]"
365
366type X = null[]
367// Message: Use "Array<null>", not "null[]"
368
369type X = Promise<{
370 foo: string,
371 bar: number
372}>[]
373// Message: Use "Array<Promise<{ foo: string, bar: number }>>", not "Promise<{ foo: string, bar: number }>[]"
374
375type X = Promise<{
376 foo: string,
377 bar: number,
378 quo: boolean
379}>[]
380// Message: Use "Array<Type>", not "Type[]"
381```
382
383The following patterns are not considered problems:
384
385```js
386type X = Array<string>
387
388// Options: ["verbose"]
389type X = Array<string>
390
391// Options: ["shorthand"]
392type X = string[]
393
394type X = Array<Array<string>>
395
396// Options: ["verbose"]
397type X = (?string)[]
398
399// Options: ["verbose"]
400// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
401type X = string[]
402
403type X = Array
404
405type X = typeof Array
406```
407
408
409
410<a name="eslint-plugin-flowtype-rules-arrow-parens"></a>
411### <code>arrow-parens</code>
412
413_The `--fix` option on the command line automatically fixes problems reported by this rule._
414
415Enforces the consistent use of parentheses in arrow functions.
416
417This rule has a string option and an object one.
418
419String options are:
420
421- `"always"` (default) requires parens around arguments in all cases.
422- `"as-needed"` enforces no braces where they can be omitted.
423
424Object properties for variants of the `"as-needed"` option:
425
426- `"requireForBlockBody": true` modifies the as-needed rule in order to require parens if the function body is in an instructions block (surrounded by braces).
427
428The following patterns are considered problems:
429
430```js
431a => {}
432// Message: undefined
433
434a => a
435// Message: undefined
436
437a => {
438}
439// Message: undefined
440
441a.then(foo => {});
442// Message: undefined
443
444a.then(foo => a);
445// Message: undefined
446
447a(foo => { if (true) {}; });
448// Message: undefined
449
450a(async foo => { if (true) {}; });
451// Message: undefined
452
453// Options: ["as-needed"]
454(a) => a
455// Message: undefined
456
457// Options: ["as-needed"]
458(a,) => a
459// Message: undefined
460
461// Options: ["as-needed"]
462async (a) => a
463// Message: undefined
464
465// Options: ["as-needed"]
466async(a) => a
467// Message: undefined
468
469// Options: ["as-needed",{"requireForBlockBody":true}]
470a => {}
471// Message: undefined
472
473// Options: ["as-needed",{"requireForBlockBody":true}]
474(a) => a
475// Message: undefined
476
477// Options: ["as-needed",{"requireForBlockBody":true}]
478async a => {}
479// Message: undefined
480
481// Options: ["as-needed",{"requireForBlockBody":true}]
482async (a) => a
483// Message: undefined
484
485// Options: ["as-needed",{"requireForBlockBody":true}]
486async(a) => a
487// Message: undefined
488```
489
490The following patterns are not considered problems:
491
492```js
493() => {}
494
495(a) => {}
496
497(a) => a
498
499(a) => {
500}
501
502a.then((foo) => {});
503
504a.then((foo) => { if (true) {}; });
505
506a.then(async (foo) => { if (true) {}; });
507
508// Options: ["always"]
509() => {}
510
511// Options: ["always"]
512(a) => {}
513
514// Options: ["always"]
515(a) => a
516
517// Options: ["always"]
518(a) => {
519}
520
521// Options: ["always"]
522a.then((foo) => {});
523
524// Options: ["always"]
525a.then((foo) => { if (true) {}; });
526
527// Options: ["always"]
528a.then(async (foo) => { if (true) {}; });
529
530// Options: ["as-needed"]
531() => {}
532
533// Options: ["as-needed"]
534a => {}
535
536// Options: ["as-needed"]
537a => a
538
539// Options: ["as-needed"]
540([a, b]) => {}
541
542// Options: ["as-needed"]
543({ a, b }) => {}
544
545// Options: ["as-needed"]
546(a = 10) => {}
547
548// Options: ["as-needed"]
549(...a) => a[0]
550
551// Options: ["as-needed"]
552(a, b) => {}
553
554// Options: ["as-needed"]
555async ([a, b]) => {}
556
557// Options: ["as-needed"]
558async (a, b) => {}
559
560// Options: ["as-needed"]
561(a: T) => a
562
563// Options: ["as-needed"]
564(a): T => a
565
566// Options: ["as-needed",{"requireForBlockBody":true}]
567() => {}
568
569// Options: ["as-needed",{"requireForBlockBody":true}]
570a => a
571
572// Options: ["as-needed",{"requireForBlockBody":true}]
573([a, b]) => {}
574
575// Options: ["as-needed",{"requireForBlockBody":true}]
576([a, b]) => a
577
578// Options: ["as-needed",{"requireForBlockBody":true}]
579({ a, b }) => {}
580
581// Options: ["as-needed",{"requireForBlockBody":true}]
582({ a, b }) => a + b
583
584// Options: ["as-needed",{"requireForBlockBody":true}]
585(a = 10) => {}
586
587// Options: ["as-needed",{"requireForBlockBody":true}]
588(...a) => a[0]
589
590// Options: ["as-needed",{"requireForBlockBody":true}]
591(a, b) => {}
592
593// Options: ["as-needed",{"requireForBlockBody":true}]
594a => ({})
595
596// Options: ["as-needed",{"requireForBlockBody":true}]
597async a => ({})
598
599// Options: ["as-needed",{"requireForBlockBody":true}]
600async a => a
601
602// Options: ["as-needed",{"requireForBlockBody":true}]
603(a: T) => a
604
605// Options: ["as-needed",{"requireForBlockBody":true}]
606(a): T => a
607
608// Options: ["always",{"requireForBlockBody":true}]
609<T>(a: T) => a
610
611// Options: ["as-needed",{"requireForBlockBody":false}]
612<T>(a: T) => { return a; }
613
614// Options: ["always",{"requireForBlockBody":true}]
615<T>(a: T) => { return a; }
616
617// Options: ["as-needed",{"requireForBlockBody":true}]
618<T>(a: T) => { return a; }
619
620// Options: ["as-needed",{"requireForBlockBody":true}]
621(a): %checks => typeof a === "number"
622```
623
624
625
626<a name="eslint-plugin-flowtype-rules-boolean-style"></a>
627### <code>boolean-style</code>
628
629_The `--fix` option on the command line automatically fixes problems reported by this rule._
630
631Enforces a particular style for boolean type annotations. This rule takes one argument.
632
633If it is `'boolean'` then a problem is raised when using `bool` instead of `boolean`.
634
635If it is `'bool'` then a problem is raised when using `boolean` instead of `bool`.
636
637The default value is `'boolean'`.
638
639The following patterns are considered problems:
640
641```js
642type X = bool
643// Message: Use "boolean", not "bool"
644
645// Options: ["boolean"]
646type X = bool
647// Message: Use "boolean", not "bool"
648
649// Options: ["bool"]
650type X = boolean
651// Message: Use "bool", not "boolean"
652```
653
654The following patterns are not considered problems:
655
656```js
657type X = boolean
658
659// Options: ["boolean"]
660type X = boolean
661
662// Options: ["bool"]
663type X = bool
664
665// Options: ["boolean"]
666// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
667type X = bool
668```
669
670
671
672<a name="eslint-plugin-flowtype-rules-define-flow-type"></a>
673### <code>define-flow-type</code>
674
675Marks Flow type identifiers as defined.
676
677Used to suppress [`no-undef`](http://eslint.org/docs/rules/no-undef) reporting of type identifiers.
678
679The following patterns are not considered problems:
680
681```js
682var a: AType
683// Additional rules: {"no-undef":2}
684
685var a: AType; var b: AType
686// Additional rules: {"no-undef":2}
687
688var a; (a: AType)
689// Additional rules: {"no-undef":2}
690
691var a: AType<BType>
692// Additional rules: {"no-undef":2}
693
694type A = AType
695// Additional rules: {"no-undef":2}
696
697declare type A = number
698// Additional rules: {"no-undef":2}
699
700opaque type A = AType
701// Additional rules: {"no-undef":2}
702
703function f(a: AType) {}
704// Additional rules: {"no-undef":2}
705
706function f(a: AType.a) {}
707// Additional rules: {"no-undef":2}
708
709function f(a: AType.a.b) {}
710// Additional rules: {"no-undef":2}
711
712function f(a): AType {}; var a: AType
713// Additional rules: {"no-undef":2}
714
715function f(a): AType {}
716// Additional rules: {"no-undef":2}
717
718class C { a: AType }
719// Additional rules: {"no-undef":2}
720
721class C { a: AType.a }
722// Additional rules: {"no-undef":2}
723
724class C { a: AType.a.b }
725// Additional rules: {"no-undef":2}
726
727class C implements AType {}
728// Additional rules: {"no-undef":2}
729
730declare interface A {}
731// Additional rules: {"no-undef":2}
732
733({ a: ({b() {}}: AType) })
734// Additional rules: {"no-undef":2}
735
736type X = {Y<AType>(): BType}
737// Additional rules: {"no-undef":2}
738
739// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
740
741/**
742* Copyright 2019 no corp
743* @flow
744*/
745type Foo = $ReadOnly<{}>
746// Additional rules: {"no-undef":2}
747
748var a: AType
749// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
750
751var a: AType; var b: AType
752// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
753
754var a; (a: AType)
755// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
756
757var a: AType<BType>
758// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
759
760type A = AType
761// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
762
763declare type A = number
764// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
765
766opaque type A = AType
767// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
768
769function f(a: AType) {}
770// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
771
772function f(a: AType.a) {}
773// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
774
775function f(a: AType.a.b) {}
776// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
777
778function f(a): AType {}; var a: AType
779// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
780
781function f(a): AType {}
782// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
783
784class C { a: AType }
785// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
786
787class C { a: AType.a }
788// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
789
790class C { a: AType.a.b }
791// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
792
793class C implements AType {}
794// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
795
796declare interface A {}
797// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
798
799({ a: ({b() {}}: AType) })
800// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
801
802type X = {Y<AType>(): BType}
803// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
804
805// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
806
807/**
808* Copyright 2019 no corp
809* @flow
810*/
811type Foo = $ReadOnly<{}>
812// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
813```
814
815
816
817<a name="eslint-plugin-flowtype-rules-delimiter-dangle"></a>
818### <code>delimiter-dangle</code>
819
820_The `--fix` option on the command line automatically fixes problems reported by this rule._
821
822Enforces consistent use of trailing commas in Object and Tuple annotations.
823
824This rule takes three arguments where the possible values are the same as ESLint's default `comma-dangle` rule:
825
8261. The first argument is for Object and Tuple annotations. The default value is `'never'`.
8272. The second argument is used for Interface annotations. This defaults to the value of the first argument.
8283. The third argument is used for inexact object notation (trailing `...`). The default value is `'never'`.
829
830If it is `'never'` then a problem is raised when there is a trailing comma.
831
832If it is `'always'` then a problem is raised when there is no trailing comma.
833
834If it is `'always-multiline'` then a problem is raised when there is no trailing comma on a multi-line definition, or there _is_ a trailing comma on a single-line definition.
835
836If it is `'only-multiline'` then a problem is raised when there is a trailing comma on a single-line definition. It allows, but does not enforce, trailing commas on multi-line definitions.
837
838The following patterns are considered problems:
839
840```js
841type X = { foo: string, }
842// Message: Unexpected trailing delimiter
843
844// Options: ["never"]
845type X = { foo: string, }
846// Message: Unexpected trailing delimiter
847
848// Options: ["never"]
849type X = { foo: string; }
850// Message: Unexpected trailing delimiter
851
852// Options: ["never"]
853type X = {
854foo: string,
855}
856// Message: Unexpected trailing delimiter
857
858// Options: ["always"]
859type X = { foo: string }
860// Message: Missing trailing delimiter
861
862// Options: ["always"]
863type X = {
864foo: string
865}
866// Message: Missing trailing delimiter
867
868// Options: ["always-multiline"]
869type X = { foo: string, }
870// Message: Unexpected trailing delimiter
871
872// Options: ["always-multiline"]
873type X = {
874foo: string
875}
876// Message: Missing trailing delimiter
877
878// Options: ["only-multiline"]
879type X = { foo: string; }
880// Message: Unexpected trailing delimiter
881
882// Options: ["always","never"]
883interface X { foo: string; }
884// Message: Unexpected trailing delimiter
885
886// Options: ["never"]
887type X = { [key: string]: number, }
888// Message: Unexpected trailing delimiter
889
890// Options: ["always"]
891type X = { [key: string]: number }
892// Message: Missing trailing delimiter
893
894// Options: ["always-multiline"]
895type X = { [key: string]: number, }
896// Message: Unexpected trailing delimiter
897
898// Options: ["always-multiline"]
899type X = {
900[key: string]: number
901}
902// Message: Missing trailing delimiter
903
904// Options: ["only-multiline"]
905type X = { [key: string]: number; }
906// Message: Unexpected trailing delimiter
907
908// Options: ["never"]
909type X = { [key: string]: number, foo: string, }
910// Message: Unexpected trailing delimiter
911
912// Options: ["never"]
913type X = {
914[key: string]: number,
915foo: string,
916}
917// Message: Unexpected trailing delimiter
918
919// Options: ["never"]
920type X = {
921[key: string]: number,
922aReallyLongPropertyNameHere: string,
923}
924// Message: Unexpected trailing delimiter
925
926// Options: ["always"]
927type X = { [key: string]: number, foo: string }
928// Message: Missing trailing delimiter
929
930// Options: ["always"]
931type X = {
932[key: string]: number;
933foo: string
934}
935// Message: Missing trailing delimiter
936
937// Options: ["always-multiline"]
938type X = { [key: string]: number, foo: string, }
939// Message: Unexpected trailing delimiter
940
941// Options: ["always-multiline"]
942type X = {
943[key: string]: number,
944foo: string
945}
946// Message: Missing trailing delimiter
947
948// Options: ["only-multiline"]
949type X = { [key: string]: number, foo: string, }
950// Message: Unexpected trailing delimiter
951
952// Options: ["never"]
953type X = { foo: string, [key: string]: number, }
954// Message: Unexpected trailing delimiter
955
956// Options: ["never"]
957type X = {
958foo: string,
959[key: string]: number,
960}
961// Message: Unexpected trailing delimiter
962
963// Options: ["never"]
964type X = {
965aReallyLongPropertyNameHere: string,
966[key: string]: number,
967}
968// Message: Unexpected trailing delimiter
969
970// Options: ["always"]
971type X = { foo: string, [key: string]: number }
972// Message: Missing trailing delimiter
973
974// Options: ["always"]
975type X = { foo: string; [key: string]: number }
976// Message: Missing trailing delimiter
977
978// Options: ["always-multiline"]
979type X = { foo: string, [key: string]: number; }
980// Message: Unexpected trailing delimiter
981
982// Options: ["always-multiline"]
983type X = {
984foo: string,
985[key: string]: number
986}
987// Message: Missing trailing delimiter
988
989// Options: ["only-multiline"]
990type X = { foo: string, [key: string]: number; }
991// Message: Unexpected trailing delimiter
992
993type X = { ..., }
994// Message: Unexpected trailing delimiter
995
996type X = { ...; }
997// Message: Unexpected trailing delimiter
998
999// Options: ["never","never","never"]
1000type X = { ..., }
1001// Message: Unexpected trailing delimiter
1002
1003// Options: ["never","never","never"]
1004type X = { ...; }
1005// Message: Unexpected trailing delimiter
1006
1007// Options: ["never","never","always"]
1008type X = { ... }
1009// Message: Missing trailing delimiter
1010
1011// Options: ["never","never","always-multiline"]
1012type X = { ..., }
1013// Message: Unexpected trailing delimiter
1014
1015// Options: ["never","never","always-multiline"]
1016type X = { ...; }
1017// Message: Unexpected trailing delimiter
1018
1019// Options: ["never","never","only-multiline"]
1020type X = { ..., }
1021// Message: Unexpected trailing delimiter
1022
1023// Options: ["never","never","only-multiline"]
1024type X = { ...; }
1025// Message: Unexpected trailing delimiter
1026
1027type X = {
1028...,
1029}
1030// Message: Unexpected trailing delimiter
1031
1032type X = {
1033...;
1034}
1035// Message: Unexpected trailing delimiter
1036
1037// Options: ["never","never","never"]
1038type X = {
1039...,
1040}
1041// Message: Unexpected trailing delimiter
1042
1043// Options: ["never","never","never"]
1044type X = {
1045...;
1046}
1047// Message: Unexpected trailing delimiter
1048
1049// Options: ["never","never","always"]
1050type X = {
1051...
1052}
1053// Message: Missing trailing delimiter
1054
1055// Options: ["never","never","always-multiline"]
1056type X = {
1057...
1058}
1059// Message: Missing trailing delimiter
1060
1061type X = { foo: string, ..., }
1062// Message: Unexpected trailing delimiter
1063
1064type X = { foo: string; ...; }
1065// Message: Unexpected trailing delimiter
1066
1067// Options: ["never","never","never"]
1068type X = { foo: string, ..., }
1069// Message: Unexpected trailing delimiter
1070
1071// Options: ["never","never","never"]
1072type X = { foo: string; ...; }
1073// Message: Unexpected trailing delimiter
1074
1075// Options: ["never","never","always"]
1076type X = { foo: string, ... }
1077// Message: Missing trailing delimiter
1078
1079// Options: ["never","never","always-multiline"]
1080type X = { foo: string, ..., }
1081// Message: Unexpected trailing delimiter
1082
1083// Options: ["never","never","always-multiline"]
1084type X = { foo: string; ...; }
1085// Message: Unexpected trailing delimiter
1086
1087// Options: ["never","never","only-multiline"]
1088type X = { foo: string, ..., }
1089// Message: Unexpected trailing delimiter
1090
1091// Options: ["never","never","only-multiline"]
1092type X = { foo: string; ...; }
1093// Message: Unexpected trailing delimiter
1094
1095type X = {
1096foo: string,
1097...,
1098}
1099// Message: Unexpected trailing delimiter
1100
1101type X = {
1102foo: string;
1103...;
1104}
1105// Message: Unexpected trailing delimiter
1106
1107// Options: ["never","never","never"]
1108type X = {
1109foo: string,
1110...,
1111}
1112// Message: Unexpected trailing delimiter
1113
1114// Options: ["never","never","never"]
1115type X = {
1116foo: string;
1117...;
1118}
1119// Message: Unexpected trailing delimiter
1120
1121// Options: ["never","never","always"]
1122type X = {
1123foo: string,
1124...
1125}
1126// Message: Missing trailing delimiter
1127
1128// Options: ["never","never","always-multiline"]
1129type X = {
1130foo: string,
1131...
1132}
1133// Message: Missing trailing delimiter
1134
1135type X = { [key: string]: number, ..., }
1136// Message: Unexpected trailing delimiter
1137
1138type X = { [key: string]: number; ...; }
1139// Message: Unexpected trailing delimiter
1140
1141// Options: ["never","never","never"]
1142type X = { [key: string]: number, ..., }
1143// Message: Unexpected trailing delimiter
1144
1145// Options: ["never","never","never"]
1146type X = { [key: string]: number; ...; }
1147// Message: Unexpected trailing delimiter
1148
1149// Options: ["never","never","always"]
1150type X = { [key: string]: number, ... }
1151// Message: Missing trailing delimiter
1152
1153// Options: ["never","never","always-multiline"]
1154type X = { [key: string]: number, ..., }
1155// Message: Unexpected trailing delimiter
1156
1157// Options: ["never","never","always-multiline"]
1158type X = { [key: string]: number; ...; }
1159// Message: Unexpected trailing delimiter
1160
1161// Options: ["never","never","only-multiline"]
1162type X = { [key: string]: number, ..., }
1163// Message: Unexpected trailing delimiter
1164
1165// Options: ["never","never","only-multiline"]
1166type X = { [key: string]: number; ...; }
1167// Message: Unexpected trailing delimiter
1168
1169type X = {
1170[key: string]: number,
1171...,
1172}
1173// Message: Unexpected trailing delimiter
1174
1175type X = {
1176[key: string]: number;
1177...;
1178}
1179// Message: Unexpected trailing delimiter
1180
1181// Options: ["never","never","never"]
1182type X = {
1183[key: string]: number,
1184...,
1185}
1186// Message: Unexpected trailing delimiter
1187
1188// Options: ["never","never","never"]
1189type X = {
1190[key: string]: number;
1191...;
1192}
1193// Message: Unexpected trailing delimiter
1194
1195// Options: ["never","never","always"]
1196type X = {
1197[key: string]: number,
1198...
1199}
1200// Message: Missing trailing delimiter
1201
1202// Options: ["never","never","always-multiline"]
1203type X = {
1204[key: string]: number,
1205...
1206}
1207// Message: Missing trailing delimiter
1208
1209type X = [string, number,]
1210// Message: Unexpected trailing delimiter
1211
1212// Options: ["never"]
1213type X = [string, number,]
1214// Message: Unexpected trailing delimiter
1215
1216// Options: ["never"]
1217type X = [
1218string,
1219number,
1220]
1221// Message: Unexpected trailing delimiter
1222
1223// Options: ["always"]
1224type X = [string, number]
1225// Message: Missing trailing delimiter
1226
1227// Options: ["always"]
1228type X = [
1229string,
1230number
1231]
1232// Message: Missing trailing delimiter
1233
1234// Options: ["always-multiline"]
1235type X = [string, number,]
1236// Message: Unexpected trailing delimiter
1237
1238// Options: ["always-multiline"]
1239type X = [
1240foo, string
1241]
1242// Message: Missing trailing delimiter
1243
1244// Options: ["only-multiline"]
1245type X = [ number, string, ]
1246// Message: Unexpected trailing delimiter
1247```
1248
1249The following patterns are not considered problems:
1250
1251```js
1252type X = { foo: string }
1253
1254// Options: ["never"]
1255type X = { foo: string }
1256
1257// Options: ["always"]
1258type X = { foo: string, }
1259
1260// Options: ["always"]
1261type X = { foo: string; }
1262
1263// Options: ["never"]
1264type X = {
1265foo: string
1266}
1267
1268// Options: ["always"]
1269type X = {
1270foo: string,
1271}
1272
1273// Options: ["always-multiline"]
1274type X = { foo: string }
1275
1276// Options: ["always-multiline"]
1277type X = {
1278foo: string,
1279}
1280
1281// Options: ["always-multiline"]
1282type X = {
1283foo: string;
1284}
1285
1286// Options: ["only-multiline"]
1287type X = { foo: string }
1288
1289// Options: ["only-multiline"]
1290type X = {
1291foo: string
1292}
1293
1294// Options: ["only-multiline"]
1295type X = {
1296foo: string,
1297}
1298
1299// Options: ["only-multiline"]
1300type X = {
1301foo: string;
1302}
1303
1304// Options: ["never","always"]
1305interface X { foo: string; }
1306
1307// Options: ["never"]
1308type X = {}
1309
1310// Options: ["always"]
1311type X = {}
1312
1313// Options: ["always-multiline"]
1314type X = {}
1315
1316// Options: ["only-multiline"]
1317type X = {}
1318
1319// Options: ["never"]
1320type X = { [key: string]: number }
1321
1322// Options: ["always"]
1323type X = { [key: string]: number, }
1324
1325// Options: ["always"]
1326type X = { [key: string]: number; }
1327
1328// Options: ["always-multiline"]
1329type X = { [key: string]: number }
1330
1331// Options: ["always-multiline"]
1332type X = {
1333[key: string]: number,
1334}
1335
1336// Options: ["only-multiline"]
1337type X = {
1338[key: string]: number,
1339}
1340
1341// Options: ["only-multiline"]
1342type X = {
1343[key: string]: number
1344}
1345
1346// Options: ["only-multiline"]
1347type X = { [key: string]: number }
1348
1349// Options: ["never"]
1350type X = { [key: string]: number, foo: string }
1351
1352// Options: ["always"]
1353type X = { [key: string]: number, foo: string, }
1354
1355// Options: ["always"]
1356type X = { [key: string]: number; foo: string; }
1357
1358// Options: ["always-multiline"]
1359type X = { [key: string]: number, foo: string }
1360
1361// Options: ["always-multiline"]
1362type X = {
1363[key: string]: number,
1364foo: string,
1365}
1366
1367// Options: ["only-multiline"]
1368type X = {
1369[key: string]: number,
1370foo: string,
1371}
1372
1373// Options: ["only-multiline"]
1374type X = {
1375[key: string]: number;
1376foo: string
1377}
1378
1379// Options: ["only-multiline"]
1380type X = { [key: string]: number, foo: string }
1381
1382// Options: ["never"]
1383type X = { foo: string, [key: string]: number }
1384
1385// Options: ["always"]
1386type X = { foo: string, [key: string]: number, }
1387
1388// Options: ["always"]
1389type X = { foo: string; [key: string]: number; }
1390
1391// Options: ["always-multiline"]
1392type X = { foo: string, [key: string]: number }
1393
1394// Options: ["always-multiline"]
1395type X = {
1396foo: string,
1397[key: string]: number,
1398}
1399
1400// Options: ["only-multiline"]
1401type X = {
1402foo: string,
1403[key: string]: number,
1404}
1405
1406// Options: ["only-multiline"]
1407type X = {
1408foo: string;
1409[key: string]: number
1410}
1411
1412// Options: ["only-multiline"]
1413type X = { foo: string, [key: string]: number }
1414
1415type X = { ... }
1416
1417// Options: ["never","never","never"]
1418type X = { ... }
1419
1420// Options: ["never","never","always"]
1421type X = { ..., }
1422
1423// Options: ["never","never","always-multiline"]
1424type X = { ... }
1425
1426// Options: ["never","never","only-multiline"]
1427type X = { ... }
1428
1429type X = {
1430...
1431}
1432
1433// Options: ["never","never","never"]
1434type X = {
1435...
1436}
1437
1438// Options: ["never","never","always"]
1439type X = {
1440...,
1441 }
1442
1443// Options: ["never","never","always"]
1444type X = {
1445...;
1446 }
1447
1448// Options: ["never","never","always-multiline"]
1449type X = {
1450...,
1451}
1452
1453// Options: ["never","never","always-multiline"]
1454type X = {
1455...;
1456}
1457
1458// Options: ["never","never","only-multiline"]
1459type X = {
1460...
1461}
1462
1463// Options: ["never","never","only-multiline"]
1464type X = {
1465...,
1466}
1467
1468// Options: ["never","never","only-multiline"]
1469type X = {
1470...;
1471}
1472
1473type X = { foo: string, ... }
1474
1475// Options: ["never","never","never"]
1476type X = { foo: string, ... }
1477
1478// Options: ["never","never","always"]
1479type X = { foo: string, ..., }
1480
1481// Options: ["never","never","always"]
1482type X = { foo: string; ...; }
1483
1484// Options: ["never","never","always-multiline"]
1485type X = { foo: string, ... }
1486
1487// Options: ["never","never","only-multiline"]
1488type X = { foo: string, ... }
1489
1490type X = {
1491foo: string,
1492...
1493}
1494
1495// Options: ["never","never","never"]
1496type X = {
1497foo: string,
1498...
1499}
1500
1501// Options: ["never","never","always"]
1502type X = {
1503foo: string,
1504...,
1505}
1506
1507// Options: ["never","never","always"]
1508type X = {
1509foo: string;
1510...;
1511}
1512
1513// Options: ["never","never","always-multiline"]
1514type X = {
1515foo: string,
1516...,
1517}
1518
1519// Options: ["never","never","always-multiline"]
1520type X = {
1521foo: string;
1522...;
1523}
1524
1525// Options: ["never","never","only-multiline"]
1526type X = {
1527foo: string,
1528...
1529}
1530
1531// Options: ["never","never","only-multiline"]
1532type X = {
1533foo: string,
1534...,
1535}
1536
1537// Options: ["never","never","only-multiline"]
1538type X = {
1539foo: string,
1540...;
1541}
1542
1543// Options: ["never","never","never"]
1544type X = { [key: string]: number, ... }
1545
1546// Options: ["never","never","always"]
1547type X = { [key: string]: number, ..., }
1548
1549// Options: ["never","never","always"]
1550type X = { [key: string]: number; ...; }
1551
1552// Options: ["never","never","always-multiline"]
1553type X = { [key: string]: number, ... }
1554
1555// Options: ["never","never","only-multiline"]
1556type X = { [key: string]: number, ... }
1557
1558// Options: ["never","never","never"]
1559type X = {
1560[key: string]: number,
1561...
1562}
1563
1564// Options: ["never","never","always"]
1565type X = {
1566[key: string]: number,
1567...,
1568}
1569
1570// Options: ["never","never","always"]
1571type X = {
1572[key: string]: number;
1573...;
1574}
1575
1576// Options: ["never","never","always-multiline"]
1577type X = {
1578[key: string]: number,
1579...,
1580}
1581
1582// Options: ["never","never","always-multiline"]
1583type X = {
1584[key: string]: number;
1585...;
1586}
1587
1588// Options: ["never","never","only-multiline"]
1589type X = {
1590[key: string]: number,
1591...
1592}
1593
1594// Options: ["never","never","only-multiline"]
1595type X = {
1596[key: string]: number,
1597...,
1598}
1599
1600// Options: ["never","never","only-multiline"]
1601type X = {
1602[key: string]: number;
1603...;
1604}
1605
1606type X = [string, number]
1607
1608// Options: ["never"]
1609type X = [string, number]
1610
1611// Options: ["never"]
1612type X = [
1613string,
1614number
1615]
1616
1617// Options: ["always"]
1618type X = [string, number,]
1619
1620// Options: ["always"]
1621type X = [
1622string,
1623number,
1624]
1625
1626// Options: ["always-multiline"]
1627type X = [ foo, string ]
1628
1629// Options: ["always-multiline"]
1630type X = [
1631foo, string,
1632]
1633
1634// Options: ["only-multiline"]
1635type X = [ number, string ]
1636
1637// Options: ["only-multiline"]
1638type X = [
1639number,
1640string
1641]
1642
1643// Options: ["only-multiline"]
1644type X = [
1645number,
1646string,
1647]
1648
1649// Options: ["never"]
1650type X = []
1651
1652// Options: ["always"]
1653type X = []
1654
1655// Options: ["always-multiline"]
1656type X = []
1657
1658// Options: ["only-multiline"]
1659type X = []
1660```
1661
1662
1663
1664<a name="eslint-plugin-flowtype-rules-generic-spacing"></a>
1665### <code>generic-spacing</code>
1666
1667_The `--fix` option on the command line automatically fixes problems reported by this rule._
1668
1669Enforces consistent spacing within generic type annotation parameters.
1670
1671This rule takes one argument. If it is `'never'` then a problem is raised when there is a space surrounding the generic type parameters. If it is `'always'` then a problem is raised when there is no space surrounding the generic type parameters.
1672
1673The default value is `'never'`.
1674
1675The following patterns are considered problems:
1676
1677```js
1678type X = Promise< string>
1679// Message: There must be no space at start of "Promise" generic type annotation
1680
1681// Options: ["never"]
1682type X = Promise< string>
1683// Message: There must be no space at start of "Promise" generic type annotation
1684
1685type X = FooBar<string >
1686// Message: There must be no space at end of "FooBar" generic type annotation
1687
1688type X = Promise< string >
1689// Message: There must be no space at start of "Promise" generic type annotation
1690// Message: There must be no space at end of "Promise" generic type annotation
1691
1692type X = Promise< (foo), bar, (((baz))) >
1693// Message: There must be no space at start of "Promise" generic type annotation
1694// Message: There must be no space at end of "Promise" generic type annotation
1695
1696// Options: ["always"]
1697type X = Promise<string >
1698// Message: There must be a space at start of "Promise" generic type annotation
1699
1700// Options: ["always"]
1701type X = FooBar< string>
1702// Message: There must be a space at end of "FooBar" generic type annotation
1703
1704// Options: ["always"]
1705type X = Promise<string>
1706// Message: There must be a space at start of "Promise" generic type annotation
1707// Message: There must be a space at end of "Promise" generic type annotation
1708
1709// Options: ["always"]
1710type X = Promise<(foo), bar, (((baz)))>
1711// Message: There must be a space at start of "Promise" generic type annotation
1712// Message: There must be a space at end of "Promise" generic type annotation
1713
1714// Options: ["always"]
1715type X = FooBar< string >
1716// Message: There must be one space at start of "FooBar" generic type annotation
1717
1718// Options: ["always"]
1719type X = FooBar< string >
1720// Message: There must be one space at end of "FooBar" generic type annotation
1721
1722// Options: ["always"]
1723type X = Promise< (foo), bar, (((baz))) >
1724// Message: There must be one space at start of "Promise" generic type annotation
1725// Message: There must be one space at end of "Promise" generic type annotation
1726```
1727
1728The following patterns are not considered problems:
1729
1730```js
1731type X = Promise<string>
1732
1733type X = Promise<(string)>
1734
1735type X = Promise<(foo), bar, (((baz)))>
1736
1737type X = Promise<
1738 (foo),
1739 bar,
1740 (((baz))),
1741>
1742
1743// Options: ["always"]
1744type X = Promise< string >
1745
1746// Options: ["always"]
1747type X = Promise< (string) >
1748
1749// Options: ["always"]
1750type X = Promise< (foo), bar, (((baz))) >
1751```
1752
1753
1754
1755<a name="eslint-plugin-flowtype-rules-newline-after-flow-annotation"></a>
1756### <code>newline-after-flow-annotation</code>
1757
1758This rule requires an empty line after the Flow annotation.
1759
1760<a name="eslint-plugin-flowtype-rules-newline-after-flow-annotation-options"></a>
1761#### Options
1762
1763The rule has a string option:
1764
1765* `"always"` (default): Enforces that `@flow` annotations be followed by an empty line, separated by newline (LF)
1766* `"always-windows"`: Identical to "always", but will use a CRLF when autofixing
1767* `"never"`: Enforces that `@flow` annotations are not followed by empty lines
1768
1769```js
1770{
1771 "rules": {
1772 "flowtype/newline-after-flow-annotation": [
1773 2,
1774 "always"
1775 ]
1776 }
1777}
1778```
1779
1780
1781The following patterns are considered problems:
1782
1783```js
1784// @flow
1785import Foo from './foo';
1786// Message: Expected newline after flow annotation
1787
1788// Options: ["always"]
1789// @flow
1790import Foo from './foo';
1791// Message: Expected newline after flow annotation
1792
1793// Options: ["always-windows"]
1794// @flow
1795import Foo from './foo';
1796// Message: Expected newline after flow annotation
1797
1798// Options: ["never"]
1799// @flow
1800
1801
1802// Message: Expected no newline after flow annotation
1803```
1804
1805The following patterns are not considered problems:
1806
1807```js
1808// Options: ["always"]
1809// @flow
1810
1811import Foo from './foo';
1812
1813// Options: ["always-windows"]
1814// @flow
1815
1816import Foo from './foo';
1817
1818// Options: ["never"]
1819// @flow
1820import Foo from './foo';
1821```
1822
1823
1824
1825<a name="eslint-plugin-flowtype-rules-no-dupe-keys"></a>
1826### <code>no-dupe-keys</code>
1827
1828Checks for duplicate properties in Object annotations.
1829
1830This rule mirrors ESLint's [no-dupe-keys](http://eslint.org/docs/rules/no-dupe-keys) rule.
1831
1832```js
1833{
1834 "rules": {
1835 "flowtype/no-dupe-keys": 2
1836 }
1837}
1838```
1839
1840The following patterns are considered problems:
1841
1842```js
1843type f = { a: number, b: string, a: number }
1844// Message: Duplicate property.
1845
1846type f = { a: number, b: string, a: string }
1847// Message: Duplicate property.
1848
1849type f = { get(key: "a"): string, get(key: "a"): string }
1850// Message: Duplicate property.
1851
1852type f = { get(key: 1): string, get(key: 1): string }
1853// Message: Duplicate property.
1854
1855type f = { get(key: 1.1): string, get(key: 1.1): string }
1856// Message: Duplicate property.
1857
1858type f = { get(key: true): string, get(key: true): string }
1859// Message: Duplicate property.
1860
1861type f = { get(key: {a: 1}): string, get(key: {a: 1}):string }
1862// Message: Duplicate property.
1863
1864var a = "a"; type f = { get(key: a): string, get(key: a): string }
1865// Message: Duplicate property.
1866
1867var b = 1; type f = { get(key: b): string, get(key: b): string }
1868// Message: Duplicate property.
1869
1870var c = true; type f = { get(key: c): string, get(key: c): string }
1871// Message: Duplicate property.
1872
1873var d = {}; type f = { get(key: d): string, get(key: d): string }
1874// Message: Duplicate property.
1875
1876var e = []; type f = { get(key: e): string, get(key: e): string }
1877// Message: Duplicate property.
1878
1879var e = [1, "a"]; type f = { get(key: e): string, get(key: e): string }
1880// Message: Duplicate property.
1881
1882function fn() {}; type f = { get(key: fn): string, get(key: fn): string }
1883// Message: Duplicate property.
1884```
1885
1886The following patterns are not considered problems:
1887
1888```js
1889type FooType = { a: number, b: string, c: number }
1890
1891// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
1892type FooType = { a: number, b: string, a: number }
1893
1894type f = { get(key: "a"): string, get(key: "b"): string }
1895
1896type f = { get(key: 1): string, get(key: 2): string }
1897
1898type f = { get(key: 1.1): string, get(key: 1.2): string }
1899
1900type f = { get(key: true): string, get(key: false): string }
1901
1902type f = { get(key: ["a", 1]): string, get(key: ["a", 2]): string }
1903
1904type f = { get(key: ["a", ["b", 1]]): string, get(key: ["a", ["b", 2]]): string }
1905
1906type f = { a: number, b: string, c: number }
1907
1908type f = { get(key: "a"): string, get(key: "b"): string }
1909
1910type f = { get(key: "a"): string, get(key: "a", key2: "b"): string }
1911
1912type f = { get(key: "a"): string, get(key: 1): string }
1913
1914type f = { get(key: { a: 1 }): string, get(key: { a: 2 }): string}
1915
1916var a = {}; var b = {}; type f = { get(key: a): string, get(key: b): string }
1917
1918var a = 1; var b = 1; type f = { get(key: a): string, get(key: b): string }
1919
1920type a = { b: <C>(config: { ...C, key: string}) => C }
1921
1922export interface Foo { get foo(): boolean; get bar(): string; }
1923```
1924
1925
1926
1927<a name="eslint-plugin-flowtype-rules-no-existential-type"></a>
1928### <code>no-existential-type</code>
1929
1930Disallows use of the existential type (*). [See more](https://flow.org/en/docs/types/utilities/#toc-existential-type)
1931
1932```js
1933{
1934 "rules": {
1935 "flowtype/no-existential-type": 2
1936 }
1937}
1938```
1939
1940
1941The following patterns are considered problems:
1942
1943```js
1944type T = *;
1945// Message: Unexpected use of existential type (*).
1946
1947type T = U<*, *>;
1948// Message: Unexpected use of existential type (*).
1949// Message: Unexpected use of existential type (*).
1950
1951const f: (*) => null = () => null;
1952// Message: Unexpected use of existential type (*).
1953```
1954
1955The following patterns are not considered problems:
1956
1957```js
1958type T = string | null
1959```
1960
1961
1962
1963<a name="eslint-plugin-flowtype-rules-no-flow-fix-me-comments"></a>
1964### <code>no-flow-fix-me-comments</code>
1965
1966Disallows `$FlowFixMe` comment suppressions.
1967
1968This is especially useful as a warning to ensure instances of `$FlowFixMe` in your codebase get fixed over time.
1969
1970<a name="eslint-plugin-flowtype-rules-no-flow-fix-me-comments-options-1"></a>
1971#### Options
1972
1973This rule takes an optional RegExp that comments a text RegExp that makes the supression valid.
1974
1975```js
1976{
1977 "rules": {
1978 "flowtype/no-flow-fix-me-comments": [
1979 1,
1980 "TODO\s+[0-9]+"
1981 ]
1982 }
1983}
1984```
1985
1986The following patterns are considered problems:
1987
1988```js
1989// $FlowFixMe I am doing something evil here
1990const text = 'HELLO';
1991// Message: $FlowFixMe is treated as `any` and should be fixed.
1992
1993// Options: ["TODO [0-9]+"]
1994// $FlowFixMe I am doing something evil here
1995const text = 'HELLO';
1996// Message: $FlowFixMe is treated as `any` and should be fixed. Fix it or match `/TODO [0-9]+/`.
1997
1998// Options: ["TODO [0-9]+"]
1999// $FlowFixMe TODO abc 47 I am doing something evil here
2000const text = 'HELLO';
2001// Message: $FlowFixMe is treated as `any` and should be fixed. Fix it or match `/TODO [0-9]+/`.
2002
2003// $$FlowFixMeProps I am doing something evil here
2004const text = 'HELLO';
2005// Message: $FlowFixMe is treated as `any` and should be fixed.
2006
2007// Options: ["TODO [0-9]+"]
2008// $FlowFixMeProps I am doing something evil here
2009const text = 'HELLO';
2010// Message: $FlowFixMe is treated as `any` and should be fixed. Fix it or match `/TODO [0-9]+/`.
2011```
2012
2013The following patterns are not considered problems:
2014
2015```js
2016const text = 'HELLO';
2017
2018// Options: ["TODO [0-9]+"]
2019// $FlowFixMe TODO 48
2020const text = 'HELLO';
2021```
2022
2023
2024
2025<a name="eslint-plugin-flowtype-rules-no-internal-flow-type"></a>
2026### <code>no-internal-flow-type</code>
2027
2028Warns against using internal Flow types such as `React$Node`, `React$Ref` and others and suggests using public alternatives instead (`React.Node`, `React.Ref`, …).
2029
2030The following patterns are considered problems:
2031
2032```js
2033type X = React$AbstractComponent<Config, Instance>
2034// Message: Type identifier 'React$AbstractComponent' is not allowed. Use 'React.AbstractComponent' instead.
2035
2036type X = React$ChildrenArray<string>
2037// Message: Type identifier 'React$ChildrenArray' is not allowed. Use 'React.ChildrenArray' instead.
2038
2039type X = React$ComponentType<Props>
2040// Message: Type identifier 'React$ComponentType' is not allowed. Use 'React.ComponentType' instead.
2041
2042type X = React$Config<Prosp, DefaultProps>
2043// Message: Type identifier 'React$Config' is not allowed. Use 'React.Config' instead.
2044
2045type X = React$Element<typeof Component>
2046// Message: Type identifier 'React$Element' is not allowed. Use 'React.Element' instead.
2047
2048type X = React$ElementConfig<typeof Component>
2049// Message: Type identifier 'React$ElementConfig' is not allowed. Use 'React.ElementConfig' instead.
2050
2051type X = React$ElementProps<typeof Component>
2052// Message: Type identifier 'React$ElementProps' is not allowed. Use 'React.ElementProps' instead.
2053
2054type X = React$ElementRef<typeof Component>
2055// Message: Type identifier 'React$ElementRef' is not allowed. Use 'React.ElementRef' instead.
2056
2057type X = React$ElementType
2058// Message: Type identifier 'React$ElementType' is not allowed. Use 'React.ElementType' instead.
2059
2060type X = React$Key
2061// Message: Type identifier 'React$Key' is not allowed. Use 'React.Key' instead.
2062
2063type X = React$Node
2064// Message: Type identifier 'React$Node' is not allowed. Use 'React.Node' instead.
2065
2066type X = React$Ref<typeof Component>
2067// Message: Type identifier 'React$Ref' is not allowed. Use 'React.Ref' instead.
2068
2069type X = React$StatelessFunctionalComponent<Props>
2070// Message: Type identifier 'React$StatelessFunctionalComponent' is not allowed. Use 'React.StatelessFunctionalComponent' instead.
2071```
2072
2073The following patterns are not considered problems:
2074
2075```js
2076type X = React.AbstractComponent<Config, Instance>
2077
2078type X = React.ChildrenArray<string>
2079
2080type X = React.ComponentType<Props>
2081
2082type X = React.Config<Props, DefaultProps>
2083
2084type X = React.Element<typeof Component>
2085
2086type X = React.ElementConfig<typeof Component>
2087
2088type X = React.ElementProps<typeof Component>
2089
2090type X = React.ElementRef<typeof Component>
2091
2092type X = React.ElementType
2093
2094type X = React.Key
2095
2096type X = React.Node
2097
2098type X = React.Ref<typeof Component>
2099
2100type X = React.StatelessFunctionalComponent<Props>
2101
2102type X = React$Rocks
2103```
2104
2105
2106
2107<a name="eslint-plugin-flowtype-rules-no-mixed"></a>
2108### <code>no-mixed</code>
2109
2110Warns against "mixed" type annotations.
2111These types are not strict enough and could often be made more specific.
2112
2113The following patterns are considered problems:
2114
2115The following patterns are considered problems:
2116
2117```js
2118function foo(thing): mixed {}
2119// Message: Unexpected use of mixed type
2120
2121function foo(thing): Promise<mixed> {}
2122// Message: Unexpected use of mixed type
2123
2124function foo(thing): Promise<Promise<mixed>> {}
2125// Message: Unexpected use of mixed type
2126```
2127
2128The following patterns are not considered problems:
2129
2130```js
2131function foo(thing): string {}
2132
2133function foo(thing): Promise<string> {}
2134
2135function foo(thing): Promise<Promise<string>> {}
2136
2137(foo?: string) => {}
2138
2139(foo: ?string) => {}
2140
2141(foo: { a: string }) => {}
2142
2143(foo: { a: ?string }) => {}
2144
2145(foo: string[]) => {}
2146
2147type Foo = string
2148
2149type Foo = { a: string }
2150
2151type Foo = { (a: string): string }
2152
2153function foo(thing: string) {}
2154
2155var foo: string
2156
2157class Foo { props: string }
2158```
2159
2160
2161
2162<a name="eslint-plugin-flowtype-rules-no-mutable-array"></a>
2163### <code>no-mutable-array</code>
2164
2165_The `--fix` option on the command line automatically fixes problems reported by this rule._
2166
2167Requires use of [`$ReadOnlyArray`](https://github.com/facebook/flow/blob/v0.46.0/lib/core.js#L185) instead of just `Array` or array [shorthand notation](https://flow.org/en/docs/types/arrays/#toc-array-type-shorthand-syntax). `$ReadOnlyArray` is immutable array collection type and the superclass of Array and tuple types in Flow. Use of `$ReadOnlyArray` instead of `Array` can solve some "problems" in typing with Flow (e.g., [1](https://github.com/facebook/flow/issues/3425), [2](https://github.com/facebook/flow/issues/4251)).
2168
2169General reasons for using immutable data structures:
2170
2171* They are simpler to construct, test, and use
2172* They help to avoid temporal coupling
2173* Their usage is side-effect free (no defensive copies)
2174* Identity mutability problem is avoided
2175* They always have failure atomicity
2176* They are much easier to cache
2177
2178Note that initialization of a variable with an empty array is considered valid (e.g., `const values: Array<string> = [];`). This behavior resembles the behavior of Flow's [unsealed objects](https://flow.org/en/docs/types/objects/#toc-unsealed-objects), as it is assumed that empty array is intended to be mutated.
2179
2180The following patterns are considered problems:
2181
2182```js
2183type X = Array<string>
2184// Message: Use "$ReadOnlyArray" instead of "Array"
2185
2186type X = string[]
2187// Message: Use "$ReadOnlyArray" instead of array shorthand notation
2188
2189const values: Array<Array<string>> = [];
2190// Message: Use "$ReadOnlyArray" instead of "Array"
2191
2192let values: Array<Array<string>>;
2193// Message: Use "$ReadOnlyArray" instead of "Array"
2194// Message: Use "$ReadOnlyArray" instead of "Array"
2195```
2196
2197The following patterns are not considered problems:
2198
2199```js
2200type X = $ReadOnlyArray<string>
2201
2202const values: Array<$ReadOnlyArray<string>> = [];
2203
2204const values: $ReadOnlyArray<string>[] = [];
2205
2206const values: Array<$ReadOnlyArray<string>> = new Array();
2207
2208const values: Array<$ReadOnlyArray<string>> = Array();
2209```
2210
2211
2212
2213<a name="eslint-plugin-flowtype-rules-no-primitive-constructor-types"></a>
2214### <code>no-primitive-constructor-types</code>
2215
2216Disallows use of primitive constructors as types, such as `Boolean`, `Number` and `String`. [See more](https://flowtype.org/docs/builtins.html).
2217
2218```js
2219{
2220 "rules": {
2221 "flowtype/no-primitive-constructor-types": 2
2222 }
2223}
2224```
2225
2226The following patterns are considered problems:
2227
2228```js
2229type x = Number
2230// Message: Unexpected use of Number constructor type.
2231
2232type x = String
2233// Message: Unexpected use of String constructor type.
2234
2235type x = Boolean
2236// Message: Unexpected use of Boolean constructor type.
2237
2238type x = { a: Number }
2239// Message: Unexpected use of Number constructor type.
2240
2241type x = { a: String }
2242// Message: Unexpected use of String constructor type.
2243
2244type x = { a: Boolean }
2245// Message: Unexpected use of Boolean constructor type.
2246
2247(x: Number) => {}
2248// Message: Unexpected use of Number constructor type.
2249
2250(x: String) => {}
2251// Message: Unexpected use of String constructor type.
2252
2253(x: Boolean) => {}
2254// Message: Unexpected use of Boolean constructor type.
2255```
2256
2257The following patterns are not considered problems:
2258
2259```js
2260type x = number
2261
2262type x = string
2263
2264type x = boolean
2265
2266type x = { a: number }
2267
2268type x = { a: string }
2269
2270type x = { a: boolean }
2271
2272(x: number) => {}
2273
2274(x: string) => {}
2275
2276(x: boolean) => {}
2277
2278type x = MyNumber
2279
2280type x = MyString
2281
2282type x = MyBoolean
2283```
2284
2285
2286
2287<a name="eslint-plugin-flowtype-rules-no-types-missing-file-annotation"></a>
2288### <code>no-types-missing-file-annotation</code>
2289
2290Disallows Flow type imports, aliases, and annotations in files missing a valid Flow file declaration (or a @noflow annotation).
2291
2292```js
2293{
2294 "rules": {
2295 "flowtype/no-types-missing-file-annotation": 2
2296 }
2297}
2298```
2299
2300The following patterns are considered problems:
2301
2302```js
2303const x: number = 42;
2304// Message: Type annotations require valid Flow declaration.
2305
2306type FooType = number;
2307// Message: Type aliases require valid Flow declaration.
2308
2309import type A from "a"
2310// Message: Type imports require valid Flow declaration.
2311
2312import type {A} from "a"
2313// Message: Type imports require valid Flow declaration.
2314
2315import {type A} from "a"
2316// Message: Type imports require valid Flow declaration.
2317
2318export type {A} from "a"
2319// Message: Type exports require valid Flow declaration.
2320
2321function t<T>(): T{}
2322// Message: Type annotations require valid Flow declaration.
2323
2324// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
2325const x: number = 42;
2326// Message: Type annotations require valid Flow declaration.
2327```
2328
2329The following patterns are not considered problems:
2330
2331```js
2332// @flow
2333const x: number = 42;
2334
2335/* @flow weak */
2336type FooType = number;
2337
2338/* @noflow */
2339type FooType = number;
2340
2341/* @noflow */
2342import type A from "a"
2343
2344/* @noflow */
2345import {type A} from "a"
2346
2347/* @noflow */
2348export type {A} from "a"
2349
2350// an unrelated comment
2351// @flow
2352export type {A} from "a"
2353```
2354
2355
2356
2357<a name="eslint-plugin-flowtype-rules-no-unused-expressions"></a>
2358### <code>no-unused-expressions</code>
2359
2360An extension of [ESLint's `no-unused-expressions`](https://eslint.org/docs/rules/no-unused-expressions).
2361This rule ignores type cast expressions and optional call expressions, but otherwise behaves the same as ESLint's
2362`no-unused-expressions`.
2363
2364Bare type casts are useful, for example to assert the exhaustiveness of a `switch`:
2365
2366```js
2367type Action
2368 = { type: 'FOO', doFoo: (_: number) => void }
2369 | { type: 'BAR', doBar: (_: string) => void };
2370
2371type State = { foo: number, bar: string };
2372
2373function runFooBar(action: Action, state: State): void {
2374 switch (action.type) {
2375 case 'FOO':
2376 doFoo(state.foo);
2377 break;
2378 case 'BAR':
2379 doBar(state.bar);
2380 break;
2381 default:
2382 (action: empty); // type error when `Action` is extended with new types
2383 console.error(`Impossible action: ${action.toString()}`);
2384 }
2385}
2386```
2387
2388This rule takes the same arguments as ESLint's `no-unused-expressions`. See
2389[that rule's documentation](https://eslint.org/docs/rules/no-unused-expressions) for details.
2390
2391The following patterns are considered problems:
2392
2393```js
2394foo + 1
2395// Message: Expected an assignment or function call and instead saw an expression.
2396
2397x?.y
2398// Message: Expected an assignment or function call and instead saw an expression.
2399```
2400
2401The following patterns are not considered problems:
2402
2403```js
2404(foo: number)
2405
2406x?.y()
2407```
2408
2409
2410
2411<a name="eslint-plugin-flowtype-rules-no-weak-types"></a>
2412### <code>no-weak-types</code>
2413
2414Warns against weak type annotations *any*, *Object* and *Function*.
2415These types can cause flow to silently skip over portions of your code,
2416which would have otherwise caused type errors.
2417
2418This rule optionally takes one argument, an object to configure which type warnings to enable. By default, all of the
2419warnings are enabled. e.g. to disable the `any` warning (allowing it to exist in your code), while continuing to warn
2420about `Object` and `Function`:
2421
2422```js
2423{
2424 "rules": {
2425 "flowtype/no-weak-types": [2, {
2426 "any": false,
2427 "Object": true,
2428 "Function": true
2429 }]
2430 }
2431}
2432
2433// or, the following is equivalent as default is true:
2434
2435{
2436 "rules": {
2437 "flowtype/no-weak-types": [2, {
2438 "any": false
2439 }]
2440 }
2441}
2442```
2443
2444The following patterns are considered problems:
2445
2446```js
2447function foo(thing): any {}
2448// Message: Unexpected use of weak type "any"
2449
2450function foo(thing): Promise<any> {}
2451// Message: Unexpected use of weak type "any"
2452
2453function foo(thing): Promise<Promise<any>> {}
2454// Message: Unexpected use of weak type "any"
2455
2456function foo(thing): Object {}
2457// Message: Unexpected use of weak type "Object"
2458
2459function foo(thing): Promise<Object> {}
2460// Message: Unexpected use of weak type "Object"
2461
2462function foo(thing): Promise<Promise<Object>> {}
2463// Message: Unexpected use of weak type "Object"
2464
2465function foo(thing): Function {}
2466// Message: Unexpected use of weak type "Function"
2467
2468function foo(thing): Promise<Function> {}
2469// Message: Unexpected use of weak type "Function"
2470
2471function foo(thing): Promise<Promise<Function>> {}
2472// Message: Unexpected use of weak type "Function"
2473
2474(foo: any) => {}
2475// Message: Unexpected use of weak type "any"
2476
2477(foo: Function) => {}
2478// Message: Unexpected use of weak type "Function"
2479
2480(foo?: any) => {}
2481// Message: Unexpected use of weak type "any"
2482
2483(foo?: Function) => {}
2484// Message: Unexpected use of weak type "Function"
2485
2486(foo: { a: any }) => {}
2487// Message: Unexpected use of weak type "any"
2488
2489(foo: { a: Object }) => {}
2490// Message: Unexpected use of weak type "Object"
2491
2492(foo: any[]) => {}
2493// Message: Unexpected use of weak type "any"
2494
2495type Foo = any
2496// Message: Unexpected use of weak type "any"
2497
2498type Foo = Function
2499// Message: Unexpected use of weak type "Function"
2500
2501type Foo = { a: any }
2502// Message: Unexpected use of weak type "any"
2503
2504type Foo = { a: Object }
2505// Message: Unexpected use of weak type "Object"
2506
2507type Foo = { (a: Object): string }
2508// Message: Unexpected use of weak type "Object"
2509
2510type Foo = { (a: string): Function }
2511// Message: Unexpected use of weak type "Function"
2512
2513function foo(thing: any) {}
2514// Message: Unexpected use of weak type "any"
2515
2516function foo(thing: Object) {}
2517// Message: Unexpected use of weak type "Object"
2518
2519var foo: Function
2520// Message: Unexpected use of weak type "Function"
2521
2522var foo: Object
2523// Message: Unexpected use of weak type "Object"
2524
2525class Foo { props: any }
2526// Message: Unexpected use of weak type "any"
2527
2528class Foo { props: Object }
2529// Message: Unexpected use of weak type "Object"
2530
2531var foo: any
2532// Message: Unexpected use of weak type "any"
2533
2534// Options: [{"Function":false}]
2535type X = any; type Y = Function; type Z = Object
2536// Message: Unexpected use of weak type "any"
2537// Message: Unexpected use of weak type "Object"
2538
2539// Options: [{"any":false,"Object":false}]
2540type X = any; type Y = Function; type Z = Object
2541// Message: Unexpected use of weak type "Function"
2542```
2543
2544The following patterns are not considered problems:
2545
2546```js
2547function foo(thing): string {}
2548
2549function foo(thing): Promise<string> {}
2550
2551function foo(thing): Promise<Promise<string>> {}
2552
2553(foo?: string) => {}
2554
2555(foo: ?string) => {}
2556
2557(foo: { a: string }) => {}
2558
2559(foo: { a: ?string }) => {}
2560
2561(foo: string[]) => {}
2562
2563type Foo = string
2564
2565type Foo = { a: string }
2566
2567type Foo = { (a: string): string }
2568
2569function foo(thing: string) {}
2570
2571var foo: string
2572
2573class Foo { props: string }
2574
2575// Options: [{"any":false,"Object":false}]
2576type X = any; type Y = Object
2577
2578// Options: [{"Function":false}]
2579type X = Function
2580
2581// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
2582function foo(thing): Function {}
2583```
2584
2585
2586
2587<a name="eslint-plugin-flowtype-rules-object-type-curly-spacing"></a>
2588### <code>object-type-curly-spacing</code>
2589
2590_The `--fix` option on the command line automatically fixes problems reported by this rule._
2591
2592This rule enforces consistent spacing inside braces of object types.
2593
2594<a name="eslint-plugin-flowtype-rules-object-type-curly-spacing-options-2"></a>
2595#### Options
2596
2597The rule has a string option:
2598
2599* `"never"` (default): disallows spacing inside of braces.
2600* `"always"`: requires spacing inside of braces.
2601
2602
2603The following patterns are considered problems:
2604
2605```js
2606type obj = { "foo": "bar" }
2607// Message: There should be no space after "{".
2608// Message: There should be no space before "}".
2609
2610type obj = {"foo": "bar" }
2611// Message: There should be no space before "}".
2612
2613type obj = {"foo": "bar", ... }
2614// Message: There should be no space before "}".
2615
2616type obj = {|"foo": "bar" |}
2617// Message: There should be no space before "|}".
2618
2619type obj = {"foo": "bar", [key: string]: string }
2620// Message: There should be no space before "}".
2621
2622type obj = { baz: {"foo": "qux"}, bar: 4}
2623// Message: There should be no space after "{".
2624
2625// Options: ["always"]
2626type obj = {"foo": "bar"}
2627// Message: A space is required after "{".
2628// Message: A space is required before "}".
2629
2630// Options: ["always"]
2631type obj = {"foo": "bar" }
2632// Message: A space is required after "{".
2633
2634// Options: ["always"]
2635type obj = { baz: {"foo": "qux"}, bar: 4}
2636// Message: A space is required before "}".
2637// Message: A space is required after "{".
2638// Message: A space is required before "}".
2639
2640// Options: ["always"]
2641type obj = { baz: { "foo": "qux" }, bar: 4}
2642// Message: A space is required before "}".
2643
2644// Options: ["always"]
2645type obj = { "foo": "bar", ...}
2646// Message: A space is required before "}".
2647
2648// Options: ["always"]
2649type obj = {|"foo": "bar" |}
2650// Message: A space is required after "{|".
2651
2652// Options: ["always"]
2653type obj = {"foo": "bar", [key: string]: string }
2654// Message: A space is required after "{".
2655```
2656
2657The following patterns are not considered problems:
2658
2659```js
2660type obj = {baz: {"foo": "qux"}, bar: 4}
2661
2662type obj = {foo: {"foo": "qux"}}
2663
2664type obj = {foo: "bar"}
2665
2666type obj = {foo: "bar"
2667}
2668
2669type obj = {
2670foo: "bar"}
2671
2672type obj = {
2673foo: "bar"}
2674
2675type obj = {|"foo": "bar"|}
2676
2677type obj = {"foo": "bar", [key: string]: string}
2678
2679// Options: ["always"]
2680type obj = { baz: { "foo": "qux" }, bar: 4 }
2681
2682// Options: ["always"]
2683type obj = {}
2684
2685// Options: ["always"]
2686type obj = {
2687foo: "bar"
2688}
2689
2690// Options: ["always"]
2691type obj = { baz: 4 }
2692
2693// Options: ["always"]
2694type obj = {| "foo": "bar" |}
2695
2696// Options: ["always"]
2697type obj = { "foo": "bar", [key: string]: string }
2698
2699// Options: ["always"]
2700type obj = { baz: { "foo": "qux" }, bar: 4 }
2701
2702// Options: ["always"]
2703type obj = {
2704 baz: { "foo": "qux" }, bar: 4
2705}
2706```
2707
2708
2709
2710<a name="eslint-plugin-flowtype-rules-object-type-delimiter"></a>
2711### <code>object-type-delimiter</code>
2712
2713_The `--fix` option on the command line automatically fixes problems reported by this rule._
2714
2715Enforces consistent separators between properties in Flow object types.
2716
2717This rule takes one argument.
2718
2719If it is `'comma'` then a problem is raised when using `;` as a separator.
2720
2721If it is `'semicolon'` then a problem is raised when using `,` as a separator.
2722
2723The default value is `'comma'`.
2724
2725_This rule is ported from `babel/flow-object-type`, however the default option was changed._
2726
2727The following patterns are considered problems:
2728
2729```js
2730// Options: ["semicolon"]
2731type Foo = { a: Foo, b: Bar }
2732// Message: Prefer semicolons to commas in object and class types
2733
2734// Options: ["comma"]
2735type Foo = { a: Foo; b: Bar }
2736// Message: Prefer commas to semicolons in object and class types
2737
2738// Options: ["semicolon"]
2739type Foo = { [a: string]: Foo, [b: string]: Bar }
2740// Message: Prefer semicolons to commas in object and class types
2741
2742// Options: ["comma"]
2743type Foo = { [a: string]: Foo; [b: string]: Bar }
2744// Message: Prefer commas to semicolons in object and class types
2745
2746// Options: ["semicolon"]
2747type Foo = { (): Foo, (): Bar }
2748// Message: Prefer semicolons to commas in object and class types
2749
2750// Options: ["comma"]
2751type Foo = { (): Foo; (): Bar }
2752// Message: Prefer commas to semicolons in object and class types
2753
2754// Options: ["semicolon"]
2755declare class Foo { a: Foo, }
2756// Message: Prefer semicolons to commas in object and class types
2757
2758// Options: ["comma"]
2759declare class Foo { a: Foo; }
2760// Message: Prefer commas to semicolons in object and class types
2761
2762// Options: ["semicolon"]
2763declare class Foo { [a: string]: Foo, }
2764// Message: Prefer semicolons to commas in object and class types
2765
2766// Options: ["comma"]
2767declare class Foo { a: Foo; }
2768// Message: Prefer commas to semicolons in object and class types
2769
2770// Options: ["semicolon"]
2771declare class Foo { (): Foo, }
2772// Message: Prefer semicolons to commas in object and class types
2773
2774// Options: ["comma"]
2775declare class Foo { (): Foo; }
2776// Message: Prefer commas to semicolons in object and class types
2777
2778// Options: ["semicolon"]
2779declare class Foo { static (): Foo, }
2780// Message: Prefer semicolons to commas in object and class types
2781
2782// Options: ["comma"]
2783declare class Foo { static (): Foo; }
2784// Message: Prefer commas to semicolons in object and class types
2785```
2786
2787The following patterns are not considered problems:
2788
2789```js
2790// Options: ["semicolon"]
2791type Foo = { a: Foo; b: Bar }
2792
2793// Options: ["comma"]
2794type Foo = { a: Foo, b: Bar }
2795
2796// Options: ["semicolon"]
2797type Foo = { [a: string]: Foo; [b: string]: Bar }
2798
2799// Options: ["comma"]
2800type Foo = { [a: string]: Foo, [b: string]: Bar }
2801
2802// Options: ["semicolon"]
2803type Foo = { (): Foo; (): Bar }
2804
2805// Options: ["comma"]
2806type Foo = { (): Foo, (): Bar }
2807
2808type Foo = { a: Foo, b: Bar }
2809
2810type Foo = { [a: string]: Foo, [b: string]: Bar }
2811
2812type Foo = { (): Foo, (): Bar }
2813
2814// Options: ["semicolon"]
2815declare class Foo { a: Foo; }
2816
2817// Options: ["comma"]
2818declare class Foo { a: Foo, }
2819
2820// Options: ["semicolon"]
2821declare class Foo { [a: string]: Foo; }
2822
2823// Options: ["comma"]
2824declare class Foo { [a: string]: Foo, }
2825
2826// Options: ["semicolon"]
2827declare class Foo { (): Foo; }
2828
2829// Options: ["comma"]
2830declare class Foo { (): Foo, }
2831
2832// Options: ["semicolon"]
2833// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
2834type Foo = { a: Foo, b: Bar }
2835```
2836
2837
2838
2839<a name="eslint-plugin-flowtype-rules-require-compound-type-alias"></a>
2840### <code>require-compound-type-alias</code>
2841
2842Requires to make a type alias for all [union](https://flow.org/en/docs/types/unions/) and [intersection](https://flow.org/en/docs/types/intersections/) types. If these are used in "raw" forms it might be tempting to just copy & paste them around the code. However, this brings sort of a source code pollution and unnecessary changes on several parts when these compound types need to be changed.
2843
2844<a name="eslint-plugin-flowtype-rules-require-compound-type-alias-options-3"></a>
2845#### Options
2846
2847The rule has two options:
2848
28491. a string option
2850
2851* `"always"` (default)
2852* `"never"`
2853
28542. an object
2855
2856```js
2857{
2858 "rules": {
2859 "flowtype/require-compound-type-alias": [
2860 2,
2861 "always",
2862 {
2863 "allowNull": true
2864 }
2865 ]
2866 }
2867}
2868```
2869
2870* `allowNull` – allows compound types where one of the members is a `null`, e.g. `string | null`.
2871
2872The following patterns are considered problems:
2873
2874```js
2875// Options: ["always",{"allowNull":false}]
2876const foo: string | null = null;
2877// Message: All union types must be declared with named type alias.
2878
2879function foo(bar: "A" | "B") {}
2880// Message: All union types must be declared with named type alias.
2881
2882const foo: "A" | "B" = "A";
2883// Message: All union types must be declared with named type alias.
2884
2885type Foo = { bar: "A" | "B" };
2886// Message: All union types must be declared with named type alias.
2887
2888function foo(bar: { n: number } | { s: string }) {}
2889// Message: All union types must be declared with named type alias.
2890
2891function foo(bar: { n: number } & { s: string }) {}
2892// Message: All intersection types must be declared with named type alias.
2893
2894const foo: { n: number } & { s: string } = { n: 0, s: "" };
2895// Message: All intersection types must be declared with named type alias.
2896
2897type Foo = { bar: { n: number } & { s: string } };
2898// Message: All intersection types must be declared with named type alias.
2899
2900function foo(bar: { n: number } & { s: string }) {}
2901// Message: All intersection types must be declared with named type alias.
2902```
2903
2904The following patterns are not considered problems:
2905
2906```js
2907const foo: string | null = null;
2908
2909// Options: ["always",{"allowNull":true}]
2910const foo: string | null = null;
2911
2912type Foo = "A" | "B";
2913
2914type Bar = "A" | "B"; function foo(bar: Bar) {}
2915
2916type Foo = { disjoint: "A", n: number } | { disjoint: "B", s: string };
2917
2918type Foo = { n: number } & { s: string };
2919
2920type Bar = { n: number } & { s: string }; function foo(bar: Bar) {}
2921
2922// Options: ["never"]
2923function foo(bar: "A" | "B") {}
2924
2925// Options: ["never"]
2926function foo(bar: { n: number } & { s: string }) {}
2927```
2928
2929
2930
2931<a name="eslint-plugin-flowtype-rules-require-exact-type"></a>
2932### <code>require-exact-type</code>
2933
2934_The `--fix` option on the command line automatically fixes problems reported by this rule._
2935
2936This rule enforces [exact object types](https://flow.org/en/docs/types/objects/#toc-exact-object-types).
2937
2938<a name="eslint-plugin-flowtype-rules-require-exact-type-options-4"></a>
2939#### Options
2940
2941The rule has one string option:
2942
2943* `"always"` (default): Report all object type definitions that aren't exact.
2944* `"never"`: Report all object type definitions that are exact.
2945
2946```js
2947{
2948 "rules": {
2949 "flowtype/require-exact-type": [
2950 2,
2951 "always"
2952 ]
2953 }
2954}
2955
2956{
2957 "rules": {
2958 "flowtype/require-exact-type": [
2959 2,
2960 "never"
2961 ]
2962 }
2963}
2964```
2965
2966The following patterns are considered problems:
2967
2968```js
2969type foo = {};
2970// Message: Object type must be exact.
2971
2972type foo = { bar: string };
2973// Message: Object type must be exact.
2974
2975// Options: ["always"]
2976type foo = Array<{bar: string}>;
2977// Message: Object type must be exact.
2978
2979// Options: ["always"]
2980(foo: Array<{bar: string}>) => {};
2981// Message: Object type must be exact.
2982
2983// Options: ["always"]
2984interface StackFrame {
2985 colno?: number;
2986 lineno?: number;
2987 filename?: string;
2988 function?: { name: string };
2989 }
2990// Message: Object type must be exact.
2991
2992// Options: ["never"]
2993type foo = {| |};
2994// Message: Object type must not be exact.
2995
2996// Options: ["never"]
2997type foo = {| bar: string |};
2998// Message: Object type must not be exact.
2999
3000// Options: ["never"]
3001type foo = { bar: {| baz: string |} };
3002// Message: Object type must not be exact.
3003
3004// Options: ["never"]
3005type foo = Array<{| bar: string |}>;
3006// Message: Object type must not be exact.
3007
3008// Options: ["never"]
3009(foo: Array<{| bar: string |}>) => {};
3010// Message: Object type must not be exact.
3011
3012// Options: ["never"]
3013interface StackFrame {
3014 colno?: number;
3015 lineno?: number;
3016 filename?: string;
3017 function?: {| name: string |};
3018 }
3019// Message: Object type must not be exact.
3020```
3021
3022The following patterns are not considered problems:
3023
3024```js
3025type foo = {| |};
3026
3027type foo = {| bar: string |};
3028
3029type foo = { [key: string]: string };
3030
3031type foo = number;
3032
3033// Options: ["always"]
3034type foo = {| |};
3035
3036// Options: ["always"]
3037type foo = {| bar: string |};
3038
3039// Options: ["always"]
3040type foo = {| bar: {| baz: string |} |};
3041
3042// Options: ["always"]
3043type foo = Array<{| bar: string |}>;
3044
3045// Options: ["always"]
3046type foo = number;
3047
3048// Options: ["always"]
3049interface StackFrame {
3050 colno?: number;
3051 lineno?: number;
3052 filename?: string;
3053 function?: {| name: string |};
3054 }
3055
3056// Options: ["never"]
3057type foo = { };
3058
3059// Options: ["never"]
3060type foo = { bar: string };
3061
3062// Options: ["never"]
3063type foo = { bar: { baz: string } };
3064
3065// Options: ["never"]
3066type foo = Array<{bar: string}>;
3067
3068// Options: ["never"]
3069type foo = number;
3070
3071// Options: ["always"]
3072interface StackFrame {
3073 colno?: number;
3074 lineno?: number;
3075 filename?: string;
3076 function?: {| name: string |};
3077 }
3078
3079type A = { a: string, ... }
3080```
3081
3082
3083
3084<a name="eslint-plugin-flowtype-rules-require-indexer-name"></a>
3085### <code>require-indexer-name</code>
3086
3087_The `--fix` option on the command line automatically fixes problems reported by this rule._
3088
3089This rule validates Flow object indexer name.
3090
3091<a name="eslint-plugin-flowtype-rules-require-indexer-name-options-5"></a>
3092#### Options
3093
3094The rule has a string option:
3095
3096* `"never"` (default): Never report files that are missing an indexer key name.
3097* `"always"`: Always report files that are missing an indexer key name.
3098
3099```js
3100{
3101 "rules": {
3102 "flowtype/require-indexer-name": [
3103 2,
3104 "always"
3105 ]
3106 }
3107}
3108```
3109
3110The following patterns are considered problems:
3111
3112```js
3113type foo = { [string]: number };
3114// Message: All indexers must be declared with key name.
3115```
3116
3117The following patterns are not considered problems:
3118
3119```js
3120type foo = { [key: string]: number };
3121
3122// Options: ["never"]
3123type foo = { [key: string]: number };
3124
3125// Options: ["never"]
3126type foo = { [string]: number };
3127```
3128
3129
3130
3131<a name="eslint-plugin-flowtype-rules-require-inexact-type"></a>
3132### <code>require-inexact-type</code>
3133
3134This rule enforces explicit inexact object types.
3135
3136<a name="eslint-plugin-flowtype-rules-require-inexact-type-options-6"></a>
3137#### Options
3138
3139The rule has one string option:
3140
3141- `"always"` (default): Report all object type definitions that aren't explicit inexact, but ignore exact objects.
3142- `"never"`: Report all object type definitions that are explicit inexact.
3143
3144```js
3145{
3146 "rules": {
3147 "flowtype/require-inexact-type": [
3148 2,
3149 "always"
3150 ]
3151 }
3152}
3153
3154{
3155 "rules": {
3156 "flowtype/require-inexact-type": [
3157 2,
3158 "never"
3159 ]
3160 }
3161}
3162```
3163
3164The following patterns are considered problems:
3165
3166```js
3167type foo = {};
3168// Message: Type must be explicit inexact.
3169
3170type foo = { bar: string };
3171// Message: Type must be explicit inexact.
3172
3173// Options: ["always"]
3174type foo = {};
3175// Message: Type must be explicit inexact.
3176
3177// Options: ["always"]
3178type foo = { bar: string };
3179// Message: Type must be explicit inexact.
3180
3181// Options: ["never"]
3182type foo = {...};
3183// Message: Type must not be explicit inexact.
3184
3185// Options: ["never"]
3186type foo = { bar: string, ... };
3187// Message: Type must not be explicit inexact.
3188```
3189
3190The following patterns are not considered problems:
3191
3192```js
3193type foo = { foo: string, ... };
3194
3195interface Foo { foo: string }
3196
3197declare class Foo { foo: string }
3198
3199type foo = {| |};
3200
3201type foo = {| bar: string |};
3202
3203type foo = { [key: string]: string, ... };
3204
3205type foo = number;
3206
3207// Options: ["always"]
3208type foo = {| |};
3209
3210// Options: ["always"]
3211type foo = {...};
3212
3213// Options: ["always"]
3214type foo = { bar: string, ... };
3215
3216// Options: ["always"]
3217type foo = {| bar: string |};
3218
3219// Options: ["always"]
3220type foo = number;
3221
3222// Options: ["never"]
3223type foo = { };
3224
3225// Options: ["never"]
3226type foo = {| |};
3227
3228// Options: ["never"]
3229type foo = { bar: string };
3230
3231// Options: ["never"]
3232type foo = {| bar: string |};
3233
3234// Options: ["never"]
3235type foo = number;
3236```
3237
3238
3239
3240<a name="eslint-plugin-flowtype-rules-require-parameter-type"></a>
3241### <code>require-parameter-type</code>
3242
3243Requires that all function parameters have type annotations.
3244
3245<a name="eslint-plugin-flowtype-rules-require-parameter-type-options-7"></a>
3246#### Options
3247
3248You can skip all arrow functions by providing the `excludeArrowFunctions` option with `true`.
3249
3250Alternatively, you can want to exclude only concise arrow functions (e.g. `x => x * 2`). Provide `excludeArrowFunctions` with `expressionsOnly` for this.
3251
3252```js
3253{
3254 "rules": {
3255 "flowtype/require-parameter-type": [
3256 2,
3257 {
3258 "excludeArrowFunctions": true
3259 }
3260 ]
3261 }
3262}
3263
3264{
3265 "rules": {
3266 "flowtype/require-parameter-type": [
3267 2,
3268 {
3269 "excludeArrowFunctions": "expressionsOnly"
3270 }
3271 ]
3272 }
3273}
3274```
3275
3276You can exclude parameters that match a certain regex by using `excludeParameterMatch`.
3277
3278```js
3279{
3280 "rules": {
3281 "flowtype/require-parameter-type": [
3282 2,
3283 {
3284 "excludeParameterMatch": "^_"
3285 }
3286 ]
3287 }
3288}
3289```
3290
3291This excludes all parameters that start with an underscore (`_`).
3292The default pattern is `a^`, which doesn't match anything, i.e., all parameters are checked.
3293
3294The following patterns are considered problems:
3295
3296```js
3297(foo) => {}
3298// Message: Missing "foo" parameter type annotation.
3299
3300function x(foo) {}
3301// Message: Missing "foo" parameter type annotation.
3302
3303// Options: [{"excludeArrowFunctions":true}]
3304function x(foo) {}
3305// Message: Missing "foo" parameter type annotation.
3306
3307(foo = 'FOO') => {}
3308// Message: Missing "foo" parameter type annotation.
3309
3310(...foo) => {}
3311// Message: Missing "foo" parameter type annotation.
3312
3313({foo}) => {}
3314// Message: Missing "{foo}" parameter type annotation.
3315
3316([foo]) => {}
3317// Message: Missing "[foo]" parameter type annotation.
3318
3319({foo = 1} = {}) => {}
3320// Message: Missing "{foo = 1}" parameter type annotation.
3321
3322// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
3323// @flow
3324(foo) => {}
3325// Message: Missing "foo" parameter type annotation.
3326
3327// Options: [{"excludeArrowFunctions":"expressionsOnly"}]
3328(foo) => {}
3329// Message: Missing "foo" parameter type annotation.
3330
3331// Options: [{"excludeArrowFunctions":"expressionsOnly"}]
3332function x(foo) {}
3333// Message: Missing "foo" parameter type annotation.
3334
3335// Options: [{"excludeParameterMatch":"^_"}]
3336(_foo: number, bar) => {}
3337// Message: Missing "bar" parameter type annotation.
3338
3339// Options: [{"excludeParameterMatch":"^_"}]
3340(_foo, bar) => {}
3341// Message: Missing "bar" parameter type annotation.
3342```
3343
3344The following patterns are not considered problems:
3345
3346```js
3347(foo: string) => {}
3348
3349(foo: string = 'FOO') => {}
3350
3351(...foo: string) => {}
3352
3353const f: Foo = (a, b) => 42;
3354
3355({foo}: {foo: string}) => {}
3356
3357([foo]: Array) => {}
3358
3359type fn = (a: string, b: number) => number;
3360const f: fn = (a, b) => {}
3361
3362// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
3363(foo) => {}
3364
3365// Options: [{"excludeArrowFunctions":true}]
3366(foo) => {}
3367
3368// Options: [{"excludeArrowFunctions":"expressionsOnly"}]
3369(foo) => 3
3370
3371// Options: [{"excludeParameterMatch":"^_"}]
3372(_foo, bar: string) => {}
3373
3374// Options: [{"excludeParameterMatch":"^_"}]
3375(_foo: number, bar: string) => {}
3376
3377// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
3378(foo) => {}
3379```
3380
3381
3382
3383<a name="eslint-plugin-flowtype-rules-require-readonly-react-props"></a>
3384### <code>require-readonly-react-props</code>
3385
3386This rule validates that React props are marked as $ReadOnly. React props are immutable and modifying them could lead to unexpected results. Marking prop shapes as $ReadOnly avoids these issues.
3387
3388The rule tries its best to work with both class and functional components. For class components, it does a fuzzy check for one of "Component", "PureComponent", "React.Component" and "React.PureComponent". It doesn't actually infer that those identifiers resolve to a proper `React.Component` object.
3389
3390For example, this will NOT be checked:
3391
3392```js
3393import MyReact from 'react';
3394class Foo extends MyReact.Component { }
3395```
3396
3397As a result, you can safely use other classes without getting warnings from this rule:
3398
3399```js
3400class MyClass extends MySuperClass { }
3401```
3402
3403React's functional components are hard to detect statically. The way it's done here is by searching for JSX within a function. When present, a function is considered a React component:
3404
3405```js
3406// this gets checked
3407type Props = { };
3408function MyComponent(props: Props) {
3409 return <p />;
3410}
3411
3412// this doesn't get checked since no JSX is present in a function
3413type Options = { };
3414function SomeHelper(options: Options) {
3415 // ...
3416}
3417
3418// this doesn't get checked since no JSX is present directly in a function
3419function helper() { return <p /> }
3420function MyComponent(props: Props) {
3421 return helper();
3422}
3423```
3424
3425The rule only works for locally defined props that are marked with a `$ReadOnly` or using covariant notation. It doesn't work with imported props:
3426
3427```js
3428// the rule has no way of knowing whether ImportedProps are read-only
3429import { type ImportedProps } from './somewhere';
3430class Foo extends React.Component<ImportedProps> { }
3431
3432
3433// the rule also checks for covariant properties
3434type Props = {|
3435 +foo: string
3436|};
3437class Bar extends React.Component<Props> { }
3438
3439// this fails because the object is not fully read-only
3440type Props = {|
3441 +foo: string,
3442 bar: number,
3443|};
3444class Bar extends React.Component<Props> { }
3445
3446// this fails because spreading makes object mutable (as of Flow 0.98)
3447// https://github.com/gajus/eslint-plugin-flowtype/pull/400#issuecomment-489813899
3448type Props = {|
3449 +foo: string,
3450 ...bar,
3451|};
3452class Bar extends React.Component<Props> { }
3453```
3454
3455
3456```js
3457{
3458 "rules": {
3459 "flowtype/require-readonly-react-props": 2
3460 }
3461}
3462```
3463
3464
3465Optionally, you can enable support for [implicit exact Flow types](https://medium.com/flow-type/on-the-roadmap-exact-objects-by-default-16b72933c5cf) (useful when using `exact_by_default=true` Flow option):
3466
3467
3468```js
3469{
3470 "rules": {
3471 "flowtype/require-readonly-react-props": [
3472 2,
3473 {
3474 "useImplicitExactTypes": true
3475 }
3476 ]
3477 }
3478}
3479```
3480
3481
3482The following patterns are considered problems:
3483
3484```js
3485type Props = { }; class Foo extends React.Component<Props> { }
3486// Message: Props must be $ReadOnly
3487
3488type OtherProps = { foo: string }; class Foo extends React.Component<OtherProps> { }
3489// Message: OtherProps must be $ReadOnly
3490
3491class Foo extends React.Component<{}> { }
3492// Message: Foo class props must be $ReadOnly
3493
3494type Props = { bar: {} }; class Foo extends React.Component<Props, State> { }
3495// Message: Props must be $ReadOnly
3496
3497type Props = { }; class Foo extends Component<Props> { }
3498// Message: Props must be $ReadOnly
3499
3500type Props = { }; class Foo extends PureComponent<Props> { }
3501// Message: Props must be $ReadOnly
3502
3503export type Props = {}; class Foo extends Component<Props> { }
3504// Message: Props must be $ReadOnly
3505
3506type Props = {| foo: string |}; class Foo extends Component<Props> { }
3507// Message: Props must be $ReadOnly
3508
3509type Props = {| +foo: string, ...bar |}; class Foo extends Component<Props> { }
3510// Message: Props must be $ReadOnly
3511
3512type Props = {| +foo: string, -bar: number |}; class Foo extends Component<Props> { }
3513// Message: Props must be $ReadOnly
3514
3515type Props = { }; function Foo(props: Props) { return <p /> }
3516// Message: Props must be $ReadOnly
3517
3518type Props = { }; function Foo(props: Props) { return foo ? <p /> : <span /> }
3519// Message: Props must be $ReadOnly
3520
3521function Foo(props: {}) { return <p /> }
3522// Message: Foo component props must be $ReadOnly
3523
3524export type Props = {}; function Foo(props: Props) { return <p /> }
3525// Message: Props must be $ReadOnly
3526```
3527
3528The following patterns are not considered problems:
3529
3530```js
3531class Foo extends React.Component<$ReadOnly<{}>> { }
3532
3533type Props = $ReadOnly<{}>; class Foo extends React.Component<Props> { }
3534
3535type Props = $ReadOnly<{}>; class Foo extends React.PureComponent<Props> { }
3536
3537class Foo extends React.Component<$ReadOnly<{}, State>> { }
3538
3539type Props = $ReadOnly<{}>; class Foo extends React.Component<Props, State> { }
3540
3541type Props = $ReadOnly<{}>; class Foo extends Component<Props> { }
3542
3543type Props = $ReadOnly<{}>; class Foo extends PureComponent<Props> { }
3544
3545type FooType = {}; class Foo extends Bar<FooType> { }
3546
3547class Foo { }
3548
3549export type Props = $ReadOnly<{}>; class Foo extends Component<Props, State> { }
3550
3551export type Props = $ReadOnly<{}>; export class Foo extends Component<Props> { }
3552
3553type Props = {| +foo: string |}; class Foo extends Component<Props> { }
3554
3555type Props = {| +foo: string, +bar: number |}; class Foo extends Component<Props> { }
3556
3557type Props = $FlowFixMe; class Foo extends Component<Props> { }
3558
3559type Props = {||}; class Foo extends Component<Props> { }
3560
3561// Options: [{"useImplicitExactTypes":true}]
3562type Props = {||}; class Foo extends Component<Props> { }
3563
3564// Options: [{"useImplicitExactTypes":true}]
3565type Props = {}; class Foo extends Component<Props> { }
3566
3567class Foo extends Component<{||}> { }
3568
3569// Options: [{"useImplicitExactTypes":true}]
3570class Foo extends Component<{||}> { }
3571
3572// Options: [{"useImplicitExactTypes":true}]
3573class Foo extends Component<{}> { }
3574
3575class Foo extends React.Component<UnknownProps> { }
3576
3577import { type Props } from "file"; class Foo extends React.Component<Props> { }
3578
3579type Props = {}; function Foo() { }
3580
3581type Props = $ReadOnly<{}>; function Foo(props: Props) { }
3582
3583type Props = {}; function Foo(props: OtherProps) { }
3584
3585function Foo() { return <p /> }
3586
3587function Foo(props: $FlowFixMe) { return <p /> }
3588
3589function Foo(props: {||}) { return <p /> }
3590
3591// Options: [{"useImplicitExactTypes":true}]
3592function Foo(props: {||}) { return <p /> }
3593
3594// Options: [{"useImplicitExactTypes":true}]
3595function Foo(props: {}) { return <p /> }
3596```
3597
3598
3599
3600<a name="eslint-plugin-flowtype-rules-require-return-type"></a>
3601### <code>require-return-type</code>
3602
3603Requires that functions have return type annotation.
3604
3605<a name="eslint-plugin-flowtype-rules-require-return-type-options-8"></a>
3606#### Options
3607
3608You can skip all arrow functions by providing the `excludeArrowFunctions` option with `true`.
3609
3610Alternatively, you can exclude a concise arrow function (e.g. `() => 2`). Provide `excludeArrowFunctions` with `expressionsOnly` for this.
3611
3612```js
3613{
3614 "rules": {
3615 "flowtype/require-return-type": [
3616 2,
3617 "always",
3618 {
3619 "excludeArrowFunctions": true
3620 }
3621 ]
3622 }
3623}
3624
3625{
3626 "rules": {
3627 "flowtype/require-return-type": [
3628 2,
3629 "always",
3630 {
3631 "excludeArrowFunctions": "expressionsOnly"
3632 }
3633 ]
3634 }
3635}
3636```
3637
3638You can exclude or include specific tests with the `includeOnlyMatching` and `excludeMatching` rules.
3639
3640```js
3641{
3642 "rules": {
3643 "flowtype/require-return-type": [
3644 2,
3645 "always",
3646 {
3647 "includeOnlyMatching": [
3648 "^F.*",
3649 "Ba(r|z)"
3650 ]
3651 }
3652 ]
3653 }
3654}
3655
3656{
3657 "rules": {
3658 "flowtype/require-return-type": [
3659 2,
3660 "always",
3661 {
3662 "excludeMatching": [
3663 "^F.*",
3664 "Ba(r|z)"
3665 ]
3666 }
3667 ]
3668 }
3669}
3670
3671```
3672
3673Both rules take an array that can contain either strings or valid RegExp statements.
3674
3675The following patterns are considered problems:
3676
3677```js
3678(foo) => { return "foo"; }
3679// Message: Missing return type annotation.
3680
3681// Options: ["always"]
3682(foo) => { return "foo"; }
3683// Message: Missing return type annotation.
3684
3685// Options: ["always"]
3686(foo) => "foo"
3687// Message: Missing return type annotation.
3688
3689(foo) => ({})
3690// Message: Missing return type annotation.
3691
3692/* @flow */
3693(foo) => { return 1; }
3694// Message: Missing return type annotation.
3695
3696(foo): undefined => { return; }
3697// Message: Must not annotate undefined return type.
3698
3699(foo): void => { return; }
3700// Message: Must not annotate undefined return type.
3701
3702(foo): undefined => { return undefined; }
3703// Message: Must not annotate undefined return type.
3704
3705(foo): void => { return void 0; }
3706// Message: Must not annotate undefined return type.
3707
3708// Options: ["always",{"annotateUndefined":"never"}]
3709(foo): undefined => { return; }
3710// Message: Must not annotate undefined return type.
3711
3712// Options: ["always",{"annotateUndefined":"never"}]
3713(foo): void => { return; }
3714// Message: Must not annotate undefined return type.
3715
3716// Options: ["always",{"annotateUndefined":"always"}]
3717(foo) => { return; }
3718// Message: Must annotate undefined return type.
3719
3720// Options: ["always",{"annotateUndefined":"never"}]
3721(foo): undefined => { return undefined; }
3722// Message: Must not annotate undefined return type.
3723
3724// Options: ["always",{"annotateUndefined":"always"}]
3725(foo) => { return undefined; }
3726// Message: Must annotate undefined return type.
3727
3728// Options: ["always",{"annotateUndefined":"always"}]
3729(foo) => { return void 0; }
3730// Message: Must annotate undefined return type.
3731
3732// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
3733// @flow
3734(foo) => { return 1; }
3735// Message: Missing return type annotation.
3736
3737// Options: ["always",{"annotateUndefined":"always"}]
3738// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
3739// @flow
3740 (foo) => { return undefined; }
3741// Message: Must annotate undefined return type.
3742
3743// Options: ["always"]
3744async () => { return 2; }
3745// Message: Missing return type annotation.
3746
3747// Options: ["always",{"annotateUndefined":"always"}]
3748async () => {}
3749// Message: Must annotate undefined return type.
3750
3751// Options: ["always",{"annotateUndefined":"always"}]
3752async function x() {}
3753// Message: Must annotate undefined return type.
3754
3755// Options: ["always",{"annotateUndefined":"never"}]
3756async (): Promise<void> => { return; }
3757// Message: Must not annotate undefined return type.
3758
3759// Options: ["always",{"annotateUndefined":"never"}]
3760async (): Promise<undefined> => { return; }
3761// Message: Must not annotate undefined return type.
3762
3763// Options: ["always",{"annotateUndefined":"always"}]
3764class Test { constructor() { } }
3765// Message: Must annotate undefined return type.
3766
3767class Test { foo() { return 42; } }
3768// Message: Missing return type annotation.
3769
3770class Test { foo = () => { return 42; } }
3771// Message: Missing return type annotation.
3772
3773class Test { foo = () => 42; }
3774// Message: Missing return type annotation.
3775
3776// Options: ["always"]
3777function* x() {}
3778// Message: Missing return type annotation.
3779
3780// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
3781() => { return 3; }
3782// Message: Missing return type annotation.
3783
3784// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
3785async () => { return 4; }
3786// Message: Missing return type annotation.
3787
3788// Options: ["always",{"includeOnlyMatching":["bar"]}]
3789function foo() { return 42; }
3790function bar() { return 42; }
3791// Message: Missing return type annotation.
3792
3793// Options: ["always",{"includeOnlyMatching":["bar"]}]
3794const foo = () => { return 42; };
3795const bar = () => { return 42; }
3796// Message: Missing return type annotation.
3797
3798// Options: ["always",{"includeOnlyMatching":["bar"]}]
3799const foo = { bar() { return 42; }, foobar: function() { return 42; } }
3800// Message: Missing return type annotation.
3801// Message: Missing return type annotation.
3802
3803// Options: ["always",{"excludeMatching":["bar"]}]
3804const foo = { bar() { return 42; }, baz() { return 42; } }
3805// Message: Missing return type annotation.
3806
3807// Options: ["always",{"annotateUndefined":"always"}]
3808function * foo() { yield 2; }
3809// Message: Missing return type annotation.
3810
3811// Options: ["always",{"annotateUndefined":"always"}]
3812async function * foo() { yield 2; }
3813// Message: Missing return type annotation.
3814```
3815
3816The following patterns are not considered problems:
3817
3818```js
3819return;
3820
3821(foo): string => {}
3822
3823const f: Foo = (a, b) => 42;
3824
3825// Options: ["always"]
3826(foo): string => {}
3827
3828type fn = (a: string, b: number) => number;
3829const f: fn = (a, b) => { return 42; }
3830
3831(foo) => { return; }
3832
3833(foo): Object => ( {} )
3834
3835(foo) => { return undefined; }
3836
3837(foo) => { return void 0; }
3838
3839// Options: ["always",{"annotateUndefined":"always"}]
3840(foo): undefined => { return; }
3841
3842// Options: ["always",{"annotateUndefined":"always"}]
3843(foo): void => { return; }
3844
3845// Options: ["always",{"annotateUndefined":"never"}]
3846(foo) => { return; }
3847
3848// Options: ["always",{"annotateUndefined":"never"}]
3849(foo) => { return undefined; }
3850
3851// Options: ["always",{"annotateUndefined":"never"}]
3852(foo) => { return void 0; }
3853
3854// Options: ["always",{"annotateUndefined":"always"}]
3855(foo): undefined => { return undefined; }
3856
3857// Options: ["always",{"annotateUndefined":"always"}]
3858(foo): void => { return void 0; }
3859
3860// Options: ["always"]
3861// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
3862(foo) => { return 1; }
3863
3864// Options: ["always"]
3865/* @noflow */
3866(foo) => { return 1; }
3867
3868// Options: ["always",{"annotateUndefined":"always"}]
3869// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
3870(foo) => { return undefined; }
3871
3872// Options: ["always",{"annotateUndefined":"always"}]
3873async function doThing(): Promise<void> {}
3874
3875// Options: ["always",{"annotateUndefined":"ignore"}]
3876async function doThing(): Promise<void> {}
3877
3878// Options: ["always",{"annotateUndefined":"ignore"}]
3879async function doThing() {}
3880
3881// Options: ["always",{"annotateUndefined":"always"}]
3882function* doThing(): Generator<number, void, void> { yield 2; }
3883
3884// Options: ["always",{"annotateUndefined":"always","excludeMatching":["constructor"]}]
3885class Test { constructor() { } }
3886
3887class Test { constructor() { } }
3888
3889// Options: ["always",{"excludeMatching":["foo"]}]
3890class Test { foo() { return 42; } }
3891
3892// Options: ["always",{"excludeMatching":["foo"]}]
3893class Test { foo = () => { return 42; } }
3894
3895// Options: ["always",{"excludeMatching":["foo"]}]
3896class Test { foo = () => 42; }
3897
3898class Test { foo = (): number => { return 42; } }
3899
3900class Test { foo = (): number => 42; }
3901
3902async (foo): Promise<number> => { return 3; }
3903
3904// Options: ["always",{"excludeArrowFunctions":true}]
3905() => 3
3906
3907// Options: ["always",{"excludeArrowFunctions":true}]
3908() => { return 4; }
3909
3910// Options: ["always",{"excludeArrowFunctions":true}]
3911() => undefined
3912
3913// Options: ["always",{"annotateUndefined":"always","excludeArrowFunctions":true}]
3914() => undefined
3915
3916// Options: ["always",{"annotateUndefined":"always","excludeArrowFunctions":true}]
3917() => { return undefined; }
3918
3919// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
3920() => 3
3921
3922// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
3923async () => 3
3924
3925// Options: ["always",{"excludeMatching":["foo"]}]
3926function foo() { return 42; }
3927
3928// Options: ["always",{"includeOnlyMatching":["bar"]}]
3929function foo() { return 42; }
3930
3931// Options: ["always",{"excludeMatching":["bar"]}]
3932function foo(): number { return 42; }
3933function bar() { return 42; }
3934
3935// Options: ["always",{"includeOnlyMatching":["foo","baz"]}]
3936function foo(): number { return 42; }
3937function bar() { return 42; }
3938
3939// Options: ["always",{"excludeMatching":["^b.*","qux"]}]
3940function foo(): number { return 42; }
3941function bar() { return 42; }
3942
3943// Options: ["always",{"includeOnlyMatching":["^f.*"]}]
3944function foo(): number { return 42; }
3945function bar() { return 42; }
3946
3947// Options: ["always",{"includeOnlyMatching":["bar"]}]
3948const foo = { baz() { return 42; } }
3949
3950// Options: ["always",{"excludeMatching":["bar"]}]
3951const foo = { bar() { return 42; } }
3952
3953// Options: ["always",{"annotateUndefined":"always"}]
3954function * foo(): Iterable<number> { yield 2; }
3955
3956// Options: ["always",{"annotateUndefined":"always"}]
3957async function * foo(): AsyncIterable<number> { yield 2; }
3958```
3959
3960
3961
3962<a name="eslint-plugin-flowtype-rules-require-types-at-top"></a>
3963### <code>require-types-at-top</code>
3964
3965Requires all type declarations to be at the top of the file, after any import declarations.
3966
3967<a name="eslint-plugin-flowtype-rules-require-types-at-top-options-9"></a>
3968#### Options
3969
3970The rule has a string option:
3971
3972* `"never"`
3973* `"always"`
3974
3975The default value is `"always"`.
3976
3977The following patterns are considered problems:
3978
3979```js
3980const foo = 3;
3981type Foo = number;
3982// Message: All type declaration should be at the top of the file, after any import declarations.
3983
3984const foo = 3;
3985opaque type Foo = number;
3986// Message: All type declaration should be at the top of the file, after any import declarations.
3987
3988const foo = 3;
3989export type Foo = number;
3990// Message: All type declaration should be at the top of the file, after any import declarations.
3991
3992const foo = 3;
3993export opaque type Foo = number;
3994// Message: All type declaration should be at the top of the file, after any import declarations.
3995
3996const foo = 3;
3997type Foo = number | string;
3998// Message: All type declaration should be at the top of the file, after any import declarations.
3999
4000import bar from "./bar";
4001const foo = 3;
4002type Foo = number;
4003// Message: All type declaration should be at the top of the file, after any import declarations.
4004```
4005
4006The following patterns are not considered problems:
4007
4008```js
4009type Foo = number;
4010const foo = 3;
4011
4012opaque type Foo = number;
4013const foo = 3;
4014
4015export type Foo = number;
4016const foo = 3;
4017
4018export opaque type Foo = number;
4019const foo = 3;
4020
4021type Foo = number;
4022const foo = 3;
4023
4024import bar from "./bar";
4025type Foo = number;
4026
4027type Foo = number;
4028import bar from "./bar";
4029
4030// Options: ["never"]
4031const foo = 3;
4032type Foo = number;
4033```
4034
4035
4036
4037<a name="eslint-plugin-flowtype-rules-require-valid-file-annotation"></a>
4038### <code>require-valid-file-annotation</code>
4039
4040This rule validates Flow file annotations.
4041
4042This rule can optionally report missing or missed placed annotations, common typos (e.g. `// @floww`), and enforce a consistent annotation style.
4043
4044<a name="eslint-plugin-flowtype-rules-require-valid-file-annotation-options-10"></a>
4045#### Options
4046
4047The rule has a string option:
4048
4049* `"never"` (default): Never report files that are missing an `@flow` annotation.
4050* `"always"`: Always report files that are missing an `@flow` annotation
4051
4052This rule has an object option:
4053
4054* `"annotationStyle"` - Enforce a consistent file annotation style.
4055 * `"none"` (default): Either annotation style is accepted.
4056 * `"line"`: Require single line annotations (i.e. `// @flow`).
4057 * `"block"`: Require block annotations (i.e. `/* @flow */`).
4058
4059* `"strict"` - Enforce a strict flow file annotation.
4060 * `false` (default): strict flow annotation is not required.
4061 * `true`: Require strict flow annotation (i.e. `// @flow strict`).
4062
4063```js
4064{
4065 "rules": {
4066 "flowtype/require-valid-file-annotation": [
4067 2,
4068 "always"
4069 ]
4070 }
4071}
4072
4073{
4074 "rules": {
4075 "flowtype/require-valid-file-annotation": [
4076 2,
4077 "always", {
4078 "annotationStyle": "block",
4079 "strict": true,
4080 }
4081 ]
4082 }
4083}
4084```
4085
4086The following patterns are considered problems:
4087
4088```js
4089// Options: ["always"]
4090#!/usr/bin/env node
4091// Message: Flow file annotation is missing.
4092
4093// Options: ["always"]
4094#!/usr/bin/env node
4095a;
4096// Message: Flow file annotation is missing.
4097
4098;// @flow
4099// Message: Flow file annotation not at the top of the file.
4100
4101;
4102// @flow
4103// Message: Flow file annotation not at the top of the file.
4104
4105// @Flow
4106// Message: Malformed Flow file annotation.
4107
4108// @NoFlow
4109// Message: Malformed Flow file annotation.
4110
4111// @Noflow
4112// Message: Malformed Flow file annotation.
4113
4114// @floweeeeeee
4115// Message: Misspelled or malformed Flow file annotation.
4116
4117// @nofloweeeeeee
4118// Message: Misspelled or malformed Flow file annotation.
4119
4120// Options: ["always"]
4121a;
4122// Message: Flow file annotation is missing.
4123
4124// Options: ["always",{"annotationStyle":"line"}]
4125/* @flow */
4126// Message: Flow file annotation style must be `// @flow`
4127
4128// Options: ["always",{"annotationStyle":"block"}]
4129// @flow
4130// Message: Flow file annotation style must be `/* @flow */`
4131
4132// Options: ["always",{"annotationStyle":"block"}]
4133// @flow
4134// Message: Flow file annotation style must be `/* @flow */`
4135
4136// Options: ["always",{"annotationStyle":"line","strict":true}]
4137// @flow
4138// Message: Strict Flow file annotation is required, should be `// @flow strict`
4139
4140// Options: ["always",{"annotationStyle":"line"}]
4141/* @noflow */
4142// Message: Flow file annotation style must be `// @noflow`
4143
4144// Options: ["always",{"annotationStyle":"block"}]
4145// @noflow
4146// Message: Flow file annotation style must be `/* @noflow */`
4147
4148// Options: ["always"]
4149a;
4150// Message: Flow file annotation is missing.
4151
4152// Options: ["always",{"annotationStyle":"block"}]
4153a;
4154// Message: Flow file annotation is missing.
4155
4156// Options: ["always",{"annotationStyle":"line","strict":true}]
4157a;
4158// Message: Flow file annotation is missing.
4159
4160// Options: ["always",{"annotationStyle":"line","strict":true}]
4161// @flow
4162a;
4163b;
4164// Message: Strict Flow file annotation is required, should be `// @flow strict`
4165
4166// Options: ["never",{"annotationStyle":"line"}]
4167/* @flow */
4168a;
4169b;
4170// Message: Flow file annotation style must be `// @flow`
4171
4172// Options: ["never",{"annotationStyle":"line"}]
4173/* @flow strict */
4174a;
4175b;
4176// Message: Flow file annotation style must be `// @flow strict`
4177```
4178
4179The following patterns are not considered problems:
4180
4181```js
4182a;
4183
4184// @flow
4185a;
4186
4187//@flow
4188a;
4189
4190//**@flow
4191a;
4192
4193/* foo @flow bar */
4194a;
4195
4196
4197
4198// @flow
4199a;
4200
4201// @flow
4202// @FLow
4203
4204// @noflow
4205a;
4206
4207// Options: ["always"]
4208// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
4209a;
4210
4211// Options: ["always",{"annotationStyle":"line"}]
4212// @flow
4213
4214// Options: ["always",{"annotationStyle":"line","strict":true}]
4215// @noflow
4216
4217// Options: ["always",{"annotationStyle":"line","strict":true}]
4218// @flow strict
4219
4220// Options: ["never",{"annotationStyle":"none"}]
4221// @function
4222
4223// Options: ["never"]
4224// @fixable
4225
4226// Options: ["always",{"annotationStyle":"block"}]
4227/* @flow */
4228```
4229
4230
4231
4232<a name="eslint-plugin-flowtype-rules-require-variable-type"></a>
4233### <code>require-variable-type</code>
4234
4235Requires that all variable declarators have type annotations.
4236
4237<a name="eslint-plugin-flowtype-rules-require-variable-type-options-11"></a>
4238#### Options
4239
4240You can exclude variables that match a certain regex by using `excludeVariableMatch`.
4241
4242This excludes all parameters that start with an underscore (`_`).
4243The default pattern is `a^`, which doesn't match anything, i.e., all parameters are checked.
4244
4245```js
4246{
4247 "rules": {
4248 "flowtype/require-variable-type": [
4249 2,
4250 {
4251 "excludeVariableMatch": "^_"
4252 }
4253 ]
4254 }
4255}
4256```
4257
4258
4259You can choose specific variable types (`var`, `let`, and `const`) to ignore using `excludeVariableTypes`.
4260
4261This excludes `var` and `let` declarations from needing type annotations, but forces `const` declarations to have it.
4262By default, all declarations are checked.
4263
4264```js
4265{
4266 "rules": {
4267 "flowtype/require-variable-type": [
4268 2,
4269 {
4270 "excludeVariableTypes": {
4271 "var": true,
4272 "let": true,
4273 "const": false,
4274 }
4275 }
4276 ]
4277 }
4278}
4279```
4280
4281
4282
4283The following patterns are considered problems:
4284
4285```js
4286var foo = "bar"
4287// Message: Missing "foo" variable type annotation.
4288
4289var foo : string = "bar", bar = 1
4290// Message: Missing "bar" variable type annotation.
4291
4292// Options: [{"excludeVariableMatch":"^_"}]
4293var _foo = "bar", bar = 1
4294// Message: Missing "bar" variable type annotation.
4295
4296// Options: [{"excludeVariableTypes":{"let":false,"var":true}}]
4297var foo = "bar", bar = 1; const oob : string = "oob"; let hey = "yah"
4298// Message: Missing "hey" variable type annotation.
4299```
4300
4301The following patterns are not considered problems:
4302
4303```js
4304var foo : string = "bar"
4305
4306var foo : string = "bar", bar : number = 1
4307
4308// Options: [{"excludeVariableMatch":"^_"}]
4309var _foo = "bar", bar : number = 1
4310
4311// Options: [{"excludeVariableTypes":{"var":true}}]
4312var foo = "bar", bar = 1
4313
4314// Options: [{"excludeVariableTypes":{"let":true,"var":true}}]
4315var foo = "bar", bar = 1; const oob : string = "oob"; let hey = "yah"
4316```
4317
4318
4319
4320<a name="eslint-plugin-flowtype-rules-semi"></a>
4321### <code>semi</code>
4322
4323_The `--fix` option on the command line automatically fixes problems reported by this rule._
4324
4325Enforces consistent use of semicolons after type aliases.
4326
4327This rule takes one argument. If it is `'never'` then a problem is raised when there is a semicolon after a type alias. If it is `'always'` then a problem is raised when there is no semicolon after a type alias.
4328
4329The default value is `'always'`.
4330
4331The following patterns are considered problems:
4332
4333```js
4334// Options: ["always"]
4335class Foo { foo: string }
4336// Message: Missing semicolon.
4337
4338// Options: ["never"]
4339class Foo { foo: string; }
4340// Message: Extra semicolon.
4341
4342// Options: []
4343type FooType = {}
4344// Message: Missing semicolon.
4345
4346// Options: ["always"]
4347type FooType = {}
4348// Message: Missing semicolon.
4349
4350// Options: ["never"]
4351type FooType = {};
4352// Message: Extra semicolon.
4353
4354// Options: []
4355opaque type FooType = {}
4356// Message: Missing semicolon.
4357```
4358
4359The following patterns are not considered problems:
4360
4361```js
4362type FooType = {};
4363
4364// Options: ["always"]
4365type FooType = {};
4366
4367// Options: ["always"]
4368(foo: string) => {}
4369
4370// Options: ["always"]
4371class Foo { foo: string; }
4372
4373// Options: ["never"]
4374class Foo { foo: string }
4375
4376// Options: ["always"]
4377type FooType = { a: number;
4378 b: string;
4379 };
4380
4381// Options: ["never"]
4382type FooType = { a: number;
4383 b: string;
4384 }
4385
4386// Options: ["never"]
4387type FooType = {}
4388
4389// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
4390type FooType = {}
4391
4392opaque type FooType = {};
4393```
4394
4395
4396
4397<a name="eslint-plugin-flowtype-rules-sort-keys"></a>
4398### <code>sort-keys</code>
4399
4400_The `--fix` option on the command line automatically fixes problems reported by this rule._
4401
4402Enforces natural, case-insensitive sorting of Object annotations.
4403
4404<a name="eslint-plugin-flowtype-rules-sort-keys-options-12"></a>
4405#### Options
4406
4407The first option specifies sort order.
4408
4409* `"asc"` (default) - enforce ascending sort order.
4410* `"desc"` - enforce descending sort order.
4411
4412```js
4413{
4414 "rules": {
4415 "flowtype/sort-keys": [
4416 2,
4417 "asc"
4418 ]
4419 }
4420}
4421```
4422
4423The following patterns are considered problems:
4424
4425```js
4426type FooType = { a: number, c: number, b: string }
4427// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4428
4429// Options: ["desc"]
4430type FooType = { a: number, b: number }
4431// Message: Expected type annotations to be in descending order. "b" should be before "a".
4432
4433// Options: ["desc"]
4434type FooType = { b: number, C: number, a: string }
4435// Message: Expected type annotations to be in descending order. "C" should be before "b".
4436
4437// Options: ["asc"]
4438type FooType = { a: number, c: number, C: number, b: string }
4439// Message: Expected type annotations to be in ascending order. "b" should be before "C".
4440
4441// Options: ["asc"]
4442type FooType = { a: number, C: number, c: number, b: string }
4443// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4444
4445// Options: ["asc"]
4446type FooType = { 1: number, 10: number, 2: boolean }
4447// Message: Expected type annotations to be in ascending order. "2" should be before "10".
4448
4449type FooType = { a: number, c: number, b: string }
4450// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4451
4452
4453 type FooType = {
4454 a: number,
4455 c: number,
4456 b: string,
4457 }
4458
4459// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4460
4461
4462 type FooType = {
4463 a: $ReadOnlyArray<number>,
4464 c: $ReadOnlyMap<string, number>,
4465 b: Map<string, Array<Map<string, number>>>,
4466 }
4467
4468// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4469
4470
4471 type FooType = {
4472 ...ErrorsInRecursiveGenericTypeArgsButDoesNotFix<{
4473 y: boolean,
4474 x: string,
4475 z: {
4476 j: string,
4477 l: number,
4478 k: boolean,
4479 },
4480 }>,
4481 a: number,
4482 c: string,
4483 b: Map<string, Array<ErrorsInRecursiveGenericTypeArgsButDoesNotFix<{
4484 y: boolean,
4485 x: string,
4486 z: {
4487 j: string,
4488 l: number,
4489 k: boolean,
4490 },
4491 }>>>,
4492 }
4493
4494// Message: Expected type annotations to be in ascending order. "x" should be before "y".
4495// Message: Expected type annotations to be in ascending order. "k" should be before "l".
4496// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4497// Message: Expected type annotations to be in ascending order. "x" should be before "y".
4498// Message: Expected type annotations to be in ascending order. "k" should be before "l".
4499
4500
4501 type FooType = {
4502 ...BPreservesSpreadOrder,
4503 ...APreservesSpreadOrder,
4504 c: string,
4505 b: number,
4506 }
4507
4508// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4509
4510
4511 type FooType = {
4512 ...BPreservesSpreadSpans,
4513 ...APreservesSpreadSpans,
4514 c: string,
4515 b: number,
4516 ...CPreservesSpreadSpans,
4517 e: string,
4518 d: number,
4519 }
4520
4521// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4522// Message: Expected type annotations to be in ascending order. "d" should be before "e".
4523
4524
4525 type FooType = {
4526 ...BPreservesSpreadOrderAndTypeArgs<string, number>,
4527 ...APreservesSpreadOrderAndTypeArgs<number>,
4528 c: string,
4529 b: number,
4530 }
4531
4532// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4533
4534
4535 type FooType = {
4536 /* preserves block comment before spread BType */
4537 // preserves line comment before spread BType
4538 ... /* preserves comment in spread BType */ BType<Generic> /* preserves trailing comment in spread AType */,
4539 /* preserves block comment before spread AType */
4540 // preserves line comment before spread AType
4541 ... /* preserves comment in spread AType */ AType /* preserves trailing comment in spread AType */,
4542 /* preserves block comment before reordered key "c" */
4543 // preserves line comment before reordered key "c"
4544 c:/* preserves comment and white space or lack of it */string/* preserves trailing comment for key "c" */,
4545 b: number,
4546 dWithoutComma: boolean
4547 }
4548
4549// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4550
4551
4552 type FooType = {
4553 +a: number,
4554 c: number,
4555 b: string,
4556 }
4557
4558// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4559
4560
4561 type FooType = {
4562 -a: number,
4563 c: number,
4564 b: string,
4565 }
4566
4567// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4568
4569
4570 type FooType = {
4571 a?: number,
4572 c: ?number,
4573 b: string,
4574 }
4575
4576// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4577
4578
4579 type FooType = {
4580 a: (number) => void,
4581 c: number,
4582 b: (param: string) => number,
4583 }
4584
4585// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4586
4587
4588 type FooType = {
4589 a: number | string | boolean,
4590 c: number,
4591 b: (param: string) => number,
4592 }
4593
4594// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4595
4596
4597 type FooType = {
4598 c: number,
4599 a: number | string | boolean,
4600 b: (param: string) => number,
4601 }
4602
4603// Message: Expected type annotations to be in ascending order. "a" should be before "c".
4604
4605
4606 type FooType = {
4607 c: {
4608 z: number,
4609 x: string,
4610 y: boolean,
4611 },
4612 a: number | string | boolean,
4613 b: (param: string) => number,
4614 }
4615
4616// Message: Expected type annotations to be in ascending order. "x" should be before "z".
4617// Message: Expected type annotations to be in ascending order. "a" should be before "c".
4618
4619
4620 type FooType = {
4621 c: {
4622 z: {
4623 j: string,
4624 l: number,
4625 k: boolean,
4626 },
4627 x: string,
4628 y: boolean,
4629 },
4630 a: number | string | boolean,
4631 b: (param: string) => number,
4632 }
4633
4634// Message: Expected type annotations to be in ascending order. "k" should be before "l".
4635// Message: Expected type annotations to be in ascending order. "x" should be before "z".
4636// Message: Expected type annotations to be in ascending order. "a" should be before "c".
4637
4638
4639 type FooType = {
4640 +c: number,
4641 -b: number,
4642 a: number,
4643 }
4644
4645// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4646// Message: Expected type annotations to be in ascending order. "a" should be before "b".
4647
4648
4649 type FooType = {|
4650 +c: number,
4651 -b: number,
4652 a: number,
4653 |}
4654
4655// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4656// Message: Expected type annotations to be in ascending order. "a" should be before "b".
4657
4658
4659 type FooType = {
4660 a(number): void,
4661 c: number,
4662 b(param: string): number,
4663 }
4664
4665// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4666
4667
4668 type FooType = {
4669 a: number | string | boolean,
4670 c: number,
4671 b(param: string): number,
4672 }
4673
4674// Message: Expected type annotations to be in ascending order. "b" should be before "c".
4675
4676
4677 type FooType = {
4678 c: number,
4679 a: number | string | boolean,
4680 b(param: string): number,
4681 }
4682
4683// Message: Expected type annotations to be in ascending order. "a" should be before "c".
4684
4685
4686 type FooType = {
4687 c: {
4688 z: number,
4689 x: string,
4690 y: boolean,
4691 },
4692 a: number | string | boolean,
4693 b(param: string): number,
4694 }
4695
4696// Message: Expected type annotations to be in ascending order. "x" should be before "z".
4697// Message: Expected type annotations to be in ascending order. "a" should be before "c".
4698
4699
4700 type FooType = {
4701 c: {
4702 z: {
4703 j: string,
4704 l: number,
4705 k: boolean,
4706 },
4707 x: string,
4708 y: boolean,
4709 },
4710 a: number | string | boolean,
4711 b(param: string): number,
4712 }
4713
4714// Message: Expected type annotations to be in ascending order. "k" should be before "l".
4715// Message: Expected type annotations to be in ascending order. "x" should be before "z".
4716// Message: Expected type annotations to be in ascending order. "a" should be before "c".
4717```
4718
4719The following patterns are not considered problems:
4720
4721```js
4722type FooType = { a: number }
4723
4724type FooType = { a: number, b: number, c: (boolean | number) }
4725
4726type FooType = { a: string, b: foo, C: number }
4727
4728type FooType = { 1: number, 2: boolean, 10: number }
4729
4730// Options: ["desc"]
4731type FooType = { c: number, b: number, a: number }
4732
4733// Options: ["desc"]
4734type FooType = { C: number, b: string, a: {} }
4735
4736// Options: ["desc"]
4737type FooType = { 10: number, 2: number, 1: boolean }
4738
4739// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
4740type FooType = { b: number, a: number }
4741
4742type FooType = { a: string, b(): number, c: boolean }
4743
4744type FooType = { a(): string, b: number, c: boolean }
4745```
4746
4747
4748
4749<a name="eslint-plugin-flowtype-rules-space-after-type-colon"></a>
4750### <code>space-after-type-colon</code>
4751
4752_The `--fix` option on the command line automatically fixes problems reported by this rule._
4753
4754Enforces consistent spacing after the type annotation colon.
4755
4756<a name="eslint-plugin-flowtype-rules-space-after-type-colon-options-13"></a>
4757#### Options
4758
4759This rule has a string argument.
4760
4761* `"always"` (default): Require a space after the type annotation colon (e.g. foo: BarType).
4762* `"never"`: Require no spaces after the type annotation colon (e.g. foo:BarType).
4763
4764This rule has an option object.
4765
4766* `"allowLineBreak"` - Allow a line break to count as a space following the annotation colon.
4767 * `"true"`: Enable
4768 * `"false"`: Disable
4769
4770{
4771 "rules": {
4772 "flowtype/space-after-type-colon": [
4773 2,
4774 "always", {
4775 "allowLineBreak": false
4776 }
4777 ]
4778 }
4779}
4780
4781The following patterns are considered problems:
4782
4783```js
4784// Options: ["never"]
4785(foo: string) => {}
4786// Message: There must be no space after "foo" parameter type annotation colon.
4787
4788// Options: ["always"]
4789(foo: string) => {}
4790// Message: There must be 1 space after "foo" parameter type annotation colon.
4791
4792// Options: ["always"]
4793(foo:(() => void)) => {}
4794// Message: There must be a space after "foo" parameter type annotation colon.
4795
4796// Options: ["never"]
4797(foo: (() => void)) => {}
4798// Message: There must be no space after "foo" parameter type annotation colon.
4799
4800// Options: ["always"]
4801(foo: (() => void)) => {}
4802// Message: There must be 1 space after "foo" parameter type annotation colon.
4803
4804({ lorem, ipsum, dolor } : SomeType) => {}
4805// Message: There must be 1 space after "{ lorem, ipsum, dolor }" parameter type annotation colon.
4806
4807(foo:{ a: string, b: number }) => {}
4808// Message: There must be a space after "foo" parameter type annotation colon.
4809
4810({ a, b } :{ a: string, b: number }) => {}
4811// Message: There must be a space after "{ a, b }" parameter type annotation colon.
4812
4813([ a, b ] :string[]) => {}
4814// Message: There must be a space after "[ a, b ]" parameter type annotation colon.
4815
4816(i?:number) => {}
4817// Message: There must be a space after "i" parameter type annotation colon.
4818
4819(i?: number) => {}
4820// Message: There must be 1 space after "i" parameter type annotation colon.
4821
4822// Options: ["never"]
4823(i?: number) => {}
4824// Message: There must be no space after "i" parameter type annotation colon.
4825
4826(foo:
4827 { a: string, b: number }) => {}
4828// Message: There must not be a line break after "foo" parameter type annotation colon.
4829
4830(foo:
4831{ a: string, b: number }) => {}
4832// Message: There must not be a line break after "foo" parameter type annotation colon.
4833
4834(foo:
4835{ a: string, b: number }) => {}
4836// Message: There must not be a line break after "foo" parameter type annotation colon.
4837
4838// Options: ["always"]
4839():Object => {}
4840// Message: There must be a space after return type colon.
4841
4842// Options: ["never"]
4843(): Object => {}
4844// Message: There must be no space after return type colon.
4845
4846// Options: ["always"]
4847(): Object => {}
4848// Message: There must be 1 space after return type colon.
4849
4850// Options: ["always"]
4851():(() => void) => {}
4852// Message: There must be a space after return type colon.
4853
4854// Options: ["never"]
4855(): (() => void) => {}
4856// Message: There must be no space after return type colon.
4857
4858// Options: ["always"]
4859(): (() => void) => {}
4860// Message: There must be 1 space after return type colon.
4861
4862// Options: ["never"]
4863export default function (foo: string) {}
4864// Message: There must be no space after "foo" parameter type annotation colon.
4865
4866// Options: ["never"]
4867function foo (foo: string) {}
4868// Message: There must be no space after "foo" parameter type annotation colon.
4869
4870// Options: ["always"]
4871(foo:string) => {}
4872// Message: There must be a space after "foo" parameter type annotation colon.
4873
4874function foo (foo:string) {}
4875// Message: There must be a space after "foo" parameter type annotation colon.
4876
4877async function foo({ lorem, ipsum, dolor }:SomeType) {}
4878// Message: There must be a space after "{ lorem, ipsum, dolor }" parameter type annotation colon.
4879
4880function x(i?:number) {}
4881// Message: There must be a space after "i" parameter type annotation colon.
4882
4883function x(i?: number) {}
4884// Message: There must be 1 space after "i" parameter type annotation colon.
4885
4886// Options: ["never"]
4887function x(i?: number) {}
4888// Message: There must be no space after "i" parameter type annotation colon.
4889
4890function a():x {}
4891// Message: There must be a space after return type colon.
4892
4893// Options: ["always"]
4894function a(): x {}
4895// Message: There must be 1 space after return type colon.
4896
4897// Options: ["never"]
4898function a(): x {}
4899// Message: There must be no space after return type colon.
4900
4901type X = (foo:number) => string
4902// Message: There must be a space after "foo" parameter type annotation colon.
4903
4904// Options: ["never"]
4905type X = (foo: number) => string
4906// Message: There must be no space after "foo" parameter type annotation colon.
4907
4908type X = (foo: number) => string
4909// Message: There must be 1 space after "foo" parameter type annotation colon.
4910
4911type X = (foo:?number) => string
4912// Message: There must be a space after "foo" parameter type annotation colon.
4913
4914type X = (foo:(number)) => string
4915// Message: There must be a space after "foo" parameter type annotation colon.
4916
4917type X = (foo:((number))) => string
4918// Message: There must be a space after "foo" parameter type annotation colon.
4919
4920type X = (foo: ((number))) => string
4921// Message: There must be 1 space after "foo" parameter type annotation colon.
4922
4923// Options: ["never"]
4924type X = (foo: ((number))) => string
4925// Message: There must be no space after "foo" parameter type annotation colon.
4926
4927type X = (foo:?(number)) => string
4928// Message: There must be a space after "foo" parameter type annotation colon.
4929
4930type TArrayPredicate = (el: T, i?:number) => boolean
4931// Message: There must be a space after "i" parameter type annotation colon.
4932
4933type TArrayPredicate = (el: T, i?: number) => boolean
4934// Message: There must be 1 space after "i" parameter type annotation colon.
4935
4936// Options: ["never"]
4937type TArrayPredicate = (el:T, i?: number) => boolean
4938// Message: There must be no space after "i" parameter type annotation colon.
4939
4940class X { foo:string }
4941// Message: There must be a space after "foo" class property type annotation colon.
4942
4943// Options: ["never"]
4944class X { foo: string }
4945// Message: There must be no space after "foo" class property type annotation colon.
4946
4947class X { foo:?string }
4948// Message: There must be a space after "foo" class property type annotation colon.
4949
4950// Options: ["never"]
4951class X { foo: ?string }
4952// Message: There must be no space after "foo" class property type annotation colon.
4953
4954class X { static foo:number }
4955// Message: There must be a space after "foo" class property type annotation colon.
4956
4957// Options: ["never"]
4958class X { static foo: number }
4959// Message: There must be no space after "foo" class property type annotation colon.
4960
4961class X { static foo :number }
4962// Message: There must be a space after "foo" class property type annotation colon.
4963
4964// Options: ["never"]
4965class X { static foo : number }
4966// Message: There must be no space after "foo" class property type annotation colon.
4967
4968declare class X { static foo:number }
4969// Message: There must be a space after "foo" type annotation colon.
4970
4971// Options: ["never"]
4972declare class X { static foo: number }
4973// Message: There must be no space after "foo" type annotation colon.
4974
4975declare class X { static foo :number }
4976// Message: There must be a space after "foo" type annotation colon.
4977
4978// Options: ["never"]
4979declare class X { static foo : number }
4980// Message: There must be no space after "foo" type annotation colon.
4981
4982class X { +foo:string }
4983// Message: There must be a space after "foo" class property type annotation colon.
4984
4985class X { +foo: string }
4986// Message: There must be 1 space after "foo" class property type annotation colon.
4987
4988// Options: ["never"]
4989class X { +foo: string }
4990// Message: There must be no space after "foo" class property type annotation colon.
4991
4992class X { static +foo:string }
4993// Message: There must be a space after "foo" class property type annotation colon.
4994
4995class X { static +foo: string }
4996// Message: There must be 1 space after "foo" class property type annotation colon.
4997
4998// Options: ["never"]
4999class X { static +foo: string }
5000// Message: There must be no space after "foo" class property type annotation colon.
5001
5002type X = { foo:string }
5003// Message: There must be a space after "foo" type annotation colon.
5004
5005// Options: ["always"]
5006type X = { foo:string }
5007// Message: There must be a space after "foo" type annotation colon.
5008
5009// Options: ["never"]
5010type X = { foo: string }
5011// Message: There must be no space after "foo" type annotation colon.
5012
5013type X = { foo: string }
5014// Message: There must be 1 space after "foo" type annotation colon.
5015
5016type X = { foo?:string }
5017// Message: There must be a space after "foo" type annotation colon.
5018
5019// Options: ["never"]
5020type X = { foo?: string }
5021// Message: There must be no space after "foo" type annotation colon.
5022
5023type X = { foo?:?string }
5024// Message: There must be a space after "foo" type annotation colon.
5025
5026type X = { foo?: ?string }
5027// Message: There must be 1 space after "foo" type annotation colon.
5028
5029type Foo = { barType:(string | () => void) }
5030// Message: There must be a space after "barType" type annotation colon.
5031
5032type Foo = { barType:(((string | () => void))) }
5033// Message: There must be a space after "barType" type annotation colon.
5034
5035// Options: ["never"]
5036type Foo = { barType: (string | () => void) }
5037// Message: There must be no space after "barType" type annotation colon.
5038
5039type Foo = { barType: (string | () => void) }
5040// Message: There must be 1 space after "barType" type annotation colon.
5041
5042type Foo = { barType: ((string | () => void)) }
5043// Message: There must be 1 space after "barType" type annotation colon.
5044
5045type X = { get:() => A; }
5046// Message: There must be a space after "get" type annotation colon.
5047
5048type X = { get:<X>() => A; }
5049// Message: There must be a space after "get" type annotation colon.
5050
5051// Options: ["never"]
5052type X = { get: () => A; }
5053// Message: There must be no space after "get" type annotation colon.
5054
5055// Options: ["never"]
5056type X = { get: <X>() => A; }
5057// Message: There must be no space after "get" type annotation colon.
5058
5059type X = { get: () => A; }
5060// Message: There must be 1 space after "get" type annotation colon.
5061
5062type X = { get: <X>() => A; }
5063// Message: There must be 1 space after "get" type annotation colon.
5064
5065type X = { +foo:string }
5066// Message: There must be a space after "foo" type annotation colon.
5067
5068type X = { +foo: string }
5069// Message: There must be 1 space after "foo" type annotation colon.
5070
5071// Options: ["never"]
5072type X = { +foo: string }
5073// Message: There must be no space after "foo" type annotation colon.
5074
5075type X = { +foo?:string }
5076// Message: There must be a space after "foo" type annotation colon.
5077
5078type X = { +foo?: string }
5079// Message: There must be 1 space after "foo" type annotation colon.
5080
5081// Options: ["never"]
5082type X = { +foo?: string }
5083// Message: There must be no space after "foo" type annotation colon.
5084
5085// Options: ["always"]
5086type X = { [a:b]: c }
5087// Message: There must be a space after type annotation colon.
5088
5089// Options: ["never"]
5090type X = { [a: b]:c }
5091// Message: There must be no space after type annotation colon.
5092
5093// Options: ["always"]
5094type X = { [a: b]: c }
5095// Message: There must be 1 space after type annotation colon.
5096
5097// Options: ["always"]
5098type X = { +[a:b]: c }
5099// Message: There must be a space after type annotation colon.
5100
5101// Options: ["never"]
5102type X = { +[a: b]:c }
5103// Message: There must be no space after type annotation colon.
5104
5105// Options: ["always"]
5106type X = { +[a: b]: c }
5107// Message: There must be 1 space after type annotation colon.
5108
5109// Options: ["always"]
5110type X = { [a: b]:c }
5111// Message: There must be a space after type annotation colon.
5112
5113// Options: ["never"]
5114type X = { [a:b]: c }
5115// Message: There must be no space after type annotation colon.
5116
5117// Options: ["always"]
5118type X = { [a: b]: c }
5119// Message: There must be 1 space after type annotation colon.
5120
5121// Options: ["always"]
5122type X = { [a:b]:c }
5123// Message: There must be a space after type annotation colon.
5124// Message: There must be a space after type annotation colon.
5125
5126// Options: ["never"]
5127type X = { [a: b]: c }
5128// Message: There must be no space after type annotation colon.
5129// Message: There must be no space after type annotation colon.
5130
5131// Options: ["always"]
5132type X = { [a: b]: c }
5133// Message: There must be 1 space after type annotation colon.
5134// Message: There must be 1 space after type annotation colon.
5135
5136// Options: ["always"]
5137type X = { [a:(b)]:(c) }
5138// Message: There must be a space after type annotation colon.
5139// Message: There must be a space after type annotation colon.
5140
5141// Options: ["never"]
5142type X = { [a: (b)]: (c) }
5143// Message: There must be no space after type annotation colon.
5144// Message: There must be no space after type annotation colon.
5145
5146// Options: ["never"]
5147const x = ({}: {})
5148// Message: There must be no space after type cast colon.
5149
5150// Options: ["always"]
5151const x = ({}:{})
5152// Message: There must be a space after type cast colon.
5153
5154// Options: ["always"]
5155const x = ({}: {})
5156// Message: There must be 1 space after type cast colon.
5157
5158// Options: ["never"]
5159((x): (string))
5160// Message: There must be no space after type cast colon.
5161
5162// Options: ["always"]
5163((x):(string))
5164// Message: There must be a space after type cast colon.
5165
5166// Options: ["always"]
5167((x): (string))
5168// Message: There must be 1 space after type cast colon.
5169
5170// Options: ["always"]
5171const x:number = 7;
5172// Message: There must be a space after const type annotation colon.
5173
5174// Options: ["always"]
5175let x:number = 42;
5176// Message: There must be a space after let type annotation colon.
5177
5178// Options: ["always"]
5179var x:number = 42;
5180// Message: There must be a space after var type annotation colon.
5181```
5182
5183The following patterns are not considered problems:
5184
5185```js
5186(foo) => {}
5187
5188(foo: string) => {}
5189
5190(foo: (string|number)) => {}
5191
5192// Options: ["never"]
5193(foo:string) => {}
5194
5195// Options: ["always"]
5196(foo: string) => {}
5197
5198// Options: ["never"]
5199(foo:(() => void)) => {}
5200
5201// Options: ["always"]
5202(foo: (() => void)) => {}
5203
5204({ lorem, ipsum, dolor }: SomeType) => {}
5205
5206(foo: { a: string, b: number }) => {}
5207
5208({ a, b }: ?{ a: string, b: number }) => {}
5209
5210([ a, b ]: string[]) => {}
5211
5212(i?: number) => {}
5213
5214// Options: ["never"]
5215(i?:number) => {}
5216
5217// Options: ["always",{"allowLineBreak":true}]
5218(foo:
5219 { a: string, b: number }) => {}
5220
5221// Options: ["always",{"allowLineBreak":true}]
5222(foo:
5223 { a: string, b: number }) => {}
5224
5225// Options: ["never"]
5226():Object => {}
5227
5228// Options: ["always"]
5229(): Object => {}
5230
5231// Options: ["never"]
5232():(number | string) => {}
5233
5234// Options: ["always"]
5235(): (number | string) => {}
5236
5237// Options: ["never"]
5238():number|string => {}
5239
5240// Options: ["always"]
5241(): number|string => {}
5242
5243// Options: ["never"]
5244():(() => void) => {}
5245
5246// Options: ["always"]
5247(): (() => void) => {}
5248
5249// Options: ["never"]
5250():( () => void ) => {}
5251
5252// Options: ["always"]
5253(): ( () => void ) => {}
5254
5255(): { a: number, b: string } => {}
5256
5257// Options: ["never"]
5258() :{ a:number, b:string } => {}
5259
5260function x(foo: string) {}
5261
5262class Foo { constructor(foo: string) {} }
5263
5264// Options: ["never"]
5265function x(foo:string) {}
5266
5267// Options: ["never"]
5268class Foo { constructor(foo:string) {} }
5269
5270async function foo({ lorem, ipsum, dolor }: SomeType) {}
5271
5272function x({ a, b }: { a: string, b: number }) {}
5273
5274function x(i?: number) {}
5275
5276// Options: ["never"]
5277function x(i?:number) {}
5278
5279function a(): x {}
5280
5281// Options: ["never"]
5282function a():x {}
5283
5284function a(): (number | string) {}
5285
5286// Options: ["never"]
5287function a() :(number | string) {}
5288
5289type X = (foo: number) => string;
5290
5291type X = (foo : number) => string;
5292
5293type X = (foo: ?number) => string;
5294
5295type X = (foo? : ?number) => string;
5296
5297type X = (foo: ?{ x: number }) => string;
5298
5299// Options: ["never"]
5300type X = (foo:number) => string;
5301
5302// Options: ["never"]
5303type X = (foo:?{ x:number }) => string;
5304
5305type X = (foo: (number)) => string
5306
5307type X = (foo: ((number))) => string
5308
5309// Options: ["never"]
5310type X = (foo:((number))) => string
5311
5312type X = ?(foo: ((number))) => string
5313
5314// Options: ["never"]
5315type X = ?(foo:((number))) => string
5316
5317type TArrayPredicate = (el: T, i?: number) => boolean
5318
5319// Options: ["never"]
5320type TArrayPredicate = (el:T, i?:number) => boolean
5321
5322type X = (number) => string;
5323
5324type X = (?number) => string;
5325
5326type X = number => string;
5327
5328type X = ?number => string;
5329
5330type X = ({ foo: bar }) => string;
5331
5332// Options: ["always"]
5333type X = (number) => string;
5334
5335// Options: ["always"]
5336type X = (?number) => string;
5337
5338// Options: ["always"]
5339type X = number => string;
5340
5341// Options: ["always"]
5342type X = ?number => string;
5343
5344// Options: ["always"]
5345type X = ({ foo: bar }) => string;
5346
5347class Foo { bar }
5348
5349class Foo { bar = 3 }
5350
5351class Foo { bar: string }
5352
5353class Foo { bar: ?string }
5354
5355// Options: ["never"]
5356class Foo { bar:string }
5357
5358// Options: ["never"]
5359class Foo { bar:?string }
5360
5361class X { static foo : number }
5362
5363// Options: ["never"]
5364class X { static foo :number }
5365
5366declare class X { static foo : number }
5367
5368// Options: ["never"]
5369declare class X { static foo :number }
5370
5371class X { +foo: string }
5372
5373class X { static +foo: string }
5374
5375// Options: ["never"]
5376class X { +foo:string }
5377
5378// Options: ["never"]
5379class X { static +foo:string }
5380
5381type X = { foo: string }
5382
5383// Options: ["never"]
5384type X = { foo:string }
5385
5386type X = { foo?: string }
5387
5388type X = { foo?: ?string }
5389
5390// Options: ["never"]
5391type X = { foo?:?string }
5392
5393type Foo = { barType: (string | () => void) }
5394
5395type Foo = { barType: ((string | () => void)) }
5396
5397// Options: ["never"]
5398type Foo = { barType:(string | () => void) }
5399
5400// Options: ["never"]
5401type Foo = { barType:((string | () => void)) }
5402
5403type X = { get(): A; }
5404
5405type X = { get<X>(): A; }
5406
5407// Options: ["never"]
5408type X = { get(): A; }
5409
5410// Options: ["never"]
5411type X = { get<X>(): A; }
5412
5413type X = { get: () => A; }
5414
5415type X = { get: <X>() => A; }
5416
5417// Options: ["never"]
5418type X = { get:() => A; }
5419
5420// Options: ["never"]
5421type X = { get:<X>() => A; }
5422
5423type X = { +foo: string }
5424
5425type X = { +foo?: string }
5426
5427// Options: ["never"]
5428type X = { +foo:string }
5429
5430// Options: ["never"]
5431type X = { +foo?:string }
5432
5433// Options: ["always"]
5434type X = { [a: b]: c }
5435
5436// Options: ["never"]
5437type X = { [a:b]:c }
5438
5439// Options: ["always"]
5440type X = { +[a: b]: c }
5441
5442// Options: ["never"]
5443type X = { +[a:b]:c }
5444
5445// Options: ["always"]
5446type X = { [string]: c }
5447
5448// Options: ["never"]
5449type X = { [string]:c }
5450
5451// Options: ["never"]
5452const x = ({}:{})
5453
5454// Options: ["always"]
5455const x = ({}: {})
5456
5457// Options: ["never"]
5458((x):(string))
5459
5460// Options: ["always"]
5461((x): (string))
5462
5463// Options: ["always"]
5464const x: number = 7;
5465
5466// Options: ["always"]
5467let x: number = 42;
5468
5469// Options: ["always"]
5470var x: number = 42;
5471```
5472
5473
5474
5475<a name="eslint-plugin-flowtype-rules-space-before-generic-bracket"></a>
5476### <code>space-before-generic-bracket</code>
5477
5478_The `--fix` option on the command line automatically fixes problems reported by this rule._
5479
5480Enforces consistent spacing before the opening `<` of generic type annotation parameters.
5481
5482This rule takes one argument. If it is `'never'` then a problem is raised when there is a space before the `<`. If it is `'always'` then a problem is raised when there is no space before the `<`.
5483
5484The default value is `'never'`.
5485
5486The following patterns are considered problems:
5487
5488```js
5489type X = Promise <string>
5490// Message: There must be no space before "Promise" generic type annotation bracket
5491
5492// Options: ["never"]
5493type X = Promise <string>
5494// Message: There must be no space before "Promise" generic type annotation bracket
5495
5496type X = Promise <string>
5497// Message: There must be no space before "Promise" generic type annotation bracket
5498
5499// Options: ["always"]
5500type X = Promise<string>
5501// Message: There must be a space before "Promise" generic type annotation bracket
5502
5503// Options: ["always"]
5504type X = Promise <string>
5505// Message: There must be one space before "Promise" generic type annotation bracket
5506```
5507
5508The following patterns are not considered problems:
5509
5510```js
5511type X = Promise<string>
5512
5513// Options: ["always"]
5514type X = Promise <string>
5515```
5516
5517
5518
5519<a name="eslint-plugin-flowtype-rules-space-before-type-colon"></a>
5520### <code>space-before-type-colon</code>
5521
5522_The `--fix` option on the command line automatically fixes problems reported by this rule._
5523
5524Enforces consistent spacing before the type annotation colon.
5525
5526This rule takes one argument. If it is `'always'` then a problem is raised when there is no space before the type annotation colon. If it is `'never'` then a problem is raised when there is a space before the type annotation colon. The default value is `'never'`.
5527
5528The following patterns are considered problems:
5529
5530```js
5531// Options: ["never"]
5532(foo : string) => {}
5533// Message: There must be no space before "foo" parameter type annotation colon.
5534
5535// Options: ["never"]
5536(foo ? : string) => {}
5537// Message: There must be no space before "foo" parameter type annotation colon.
5538
5539// Options: ["always"]
5540(foo: string) => {}
5541// Message: There must be a space before "foo" parameter type annotation colon.
5542
5543// Options: ["always"]
5544(foo : string) => {}
5545// Message: There must be 1 space before "foo" parameter type annotation colon.
5546
5547// Options: ["always"]
5548(foo?: string) => {}
5549// Message: There must be a space before "foo" parameter type annotation colon.
5550
5551// Options: ["always"]
5552(foo ? : string) => {}
5553// Message: There must be 1 space before "foo" parameter type annotation colon.
5554
5555// Options: ["always"]
5556(foo ?: string) => {}
5557// Message: There must be a space before "foo" parameter type annotation colon.
5558
5559({ lorem, ipsum, dolor } : SomeType) => {}
5560// Message: There must be no space before "{ lorem, ipsum, dolor }" parameter type annotation colon.
5561
5562(foo : { a: string, b: number }) => {}
5563// Message: There must be no space before "foo" parameter type annotation colon.
5564
5565({ a, b } : { a: string, b: number }) => {}
5566// Message: There must be no space before "{ a, b }" parameter type annotation colon.
5567
5568([ a, b ] : string[]) => {}
5569// Message: There must be no space before "[ a, b ]" parameter type annotation colon.
5570
5571() : x => {}
5572// Message: There must be no space before return type colon.
5573
5574// Options: ["always"]
5575(): x => {}
5576// Message: There must be a space before return type colon.
5577
5578// Options: ["always"]
5579() : x => {}
5580// Message: There must be 1 space before return type colon.
5581
5582function x(foo : string) {}
5583// Message: There must be no space before "foo" parameter type annotation colon.
5584
5585// Options: ["always"]
5586function x(foo: string) {}
5587// Message: There must be a space before "foo" parameter type annotation colon.
5588
5589var x = function (foo : string) {}
5590// Message: There must be no space before "foo" parameter type annotation colon.
5591
5592// Options: ["always"]
5593var x = function (foo: string) {}
5594// Message: There must be a space before "foo" parameter type annotation colon.
5595
5596class Foo { constructor(foo : string ) {} }
5597// Message: There must be no space before "foo" parameter type annotation colon.
5598
5599// Options: ["always"]
5600class Foo { constructor(foo: string ) {} }
5601// Message: There must be a space before "foo" parameter type annotation colon.
5602
5603async function foo({ lorem, ipsum, dolor } : SomeType) {}
5604// Message: There must be no space before "{ lorem, ipsum, dolor }" parameter type annotation colon.
5605
5606function a() : x {}
5607// Message: There must be no space before return type colon.
5608
5609// Options: ["always"]
5610function a(): x {}
5611// Message: There must be a space before return type colon.
5612
5613// Options: ["always"]
5614function a() : x {}
5615// Message: There must be 1 space before return type colon.
5616
5617type X = (foo :string) => string;
5618// Message: There must be no space before "foo" parameter type annotation colon.
5619
5620// Options: ["always"]
5621type X = (foo:string) => string;
5622// Message: There must be a space before "foo" parameter type annotation colon.
5623
5624// Options: ["always"]
5625type X = (foo :string) => string;
5626// Message: There must be 1 space before "foo" parameter type annotation colon.
5627
5628type X = (foo? :string) => string;
5629// Message: There must be no space before "foo" parameter type annotation colon.
5630
5631type X = (foo? :string) => string;
5632// Message: There must be no space before "foo" parameter type annotation colon.
5633
5634// Options: ["always"]
5635type X = (foo?:string) => string;
5636// Message: There must be a space before "foo" parameter type annotation colon.
5637
5638type X = (foo? :?string) => string;
5639// Message: There must be no space before "foo" parameter type annotation colon.
5640
5641class X { foo :string }
5642// Message: There must be no space before "foo" class property type annotation colon.
5643
5644// Options: ["always"]
5645class X { foo: string }
5646// Message: There must be a space before "foo" class property type annotation colon.
5647
5648class X { foo :?string }
5649// Message: There must be no space before "foo" class property type annotation colon.
5650
5651// Options: ["always"]
5652class X { foo: ?string }
5653// Message: There must be a space before "foo" class property type annotation colon.
5654
5655class X { static foo : number }
5656// Message: There must be no space before "foo" class property type annotation colon.
5657
5658class X { static foo :number }
5659// Message: There must be no space before "foo" class property type annotation colon.
5660
5661// Options: ["always"]
5662class X { static foo: number }
5663// Message: There must be a space before "foo" class property type annotation colon.
5664
5665// Options: ["always"]
5666class X { static foo:number }
5667// Message: There must be a space before "foo" class property type annotation colon.
5668
5669declare class Foo { static bar :number; }
5670// Message: There must be no space before "bar" type annotation colon.
5671
5672declare class Foo { static bar : number; }
5673// Message: There must be no space before "bar" type annotation colon.
5674
5675// Options: ["always"]
5676declare class Foo { static bar:number; }
5677// Message: There must be a space before "bar" type annotation colon.
5678
5679// Options: ["always"]
5680declare class Foo { static bar: number; }
5681// Message: There must be a space before "bar" type annotation colon.
5682
5683// Options: ["always"]
5684class X { +foo: string }
5685// Message: There must be a space before "foo" class property type annotation colon.
5686
5687// Options: ["always"]
5688class X { +foo : string }
5689// Message: There must be 1 space before "foo" class property type annotation colon.
5690
5691// Options: ["never"]
5692class X { +foo : string }
5693// Message: There must be no space before "foo" class property type annotation colon.
5694
5695// Options: ["always"]
5696class X { static +foo: string }
5697// Message: There must be a space before "foo" class property type annotation colon.
5698
5699// Options: ["always"]
5700class X { static +foo : string }
5701// Message: There must be 1 space before "foo" class property type annotation colon.
5702
5703// Options: ["never"]
5704class X { static +foo : string }
5705// Message: There must be no space before "foo" class property type annotation colon.
5706
5707type X = { foo : string }
5708// Message: There must be no space before "foo" type annotation colon.
5709
5710// Options: ["never"]
5711type X = { foo : string }
5712// Message: There must be no space before "foo" type annotation colon.
5713
5714// Options: ["always"]
5715type X = { foo: string }
5716// Message: There must be a space before "foo" type annotation colon.
5717
5718// Options: ["always"]
5719type X = { foo : string }
5720// Message: There must be 1 space before "foo" type annotation colon.
5721
5722type X = { foo? : string }
5723// Message: There must be no space before "foo" type annotation colon.
5724
5725// Options: ["always"]
5726type X = { foo?: string }
5727// Message: There must be a space before "foo" type annotation colon.
5728
5729// Options: ["always"]
5730type X = { foo? : string }
5731// Message: There must be 1 space before "foo" type annotation colon.
5732
5733// Options: ["always"]
5734type X = { foo ?: string }
5735// Message: There must be a space before "foo" type annotation colon.
5736
5737// Options: ["always"]
5738type X = { +foo: string }
5739// Message: There must be a space before "foo" type annotation colon.
5740
5741// Options: ["always"]
5742type X = { +foo : string }
5743// Message: There must be 1 space before "foo" type annotation colon.
5744
5745// Options: ["never"]
5746type X = { +foo : string }
5747// Message: There must be no space before "foo" type annotation colon.
5748
5749// Options: ["always"]
5750type X = { +foo?: string }
5751// Message: There must be a space before "foo" type annotation colon.
5752
5753// Options: ["always"]
5754type X = { +foo? : string }
5755// Message: There must be 1 space before "foo" type annotation colon.
5756
5757// Options: ["never"]
5758type X = { +foo? : string }
5759// Message: There must be no space before "foo" type annotation colon.
5760
5761// Options: ["always"]
5762type X = { [a: b] : c }
5763// Message: There must be a space before type annotation colon.
5764
5765// Options: ["never"]
5766type X = { [a : b]: c }
5767// Message: There must be no space before type annotation colon.
5768
5769// Options: ["always"]
5770type X = { [a : b] : c }
5771// Message: There must be 1 space before type annotation colon.
5772
5773// Options: ["always"]
5774type X = { +[a:b] : c }
5775// Message: There must be a space before type annotation colon.
5776
5777// Options: ["never"]
5778type X = { +[a : b]: c }
5779// Message: There must be no space before type annotation colon.
5780
5781// Options: ["always"]
5782type X = { +[a : b] : c }
5783// Message: There must be 1 space before type annotation colon.
5784
5785// Options: ["always"]
5786type X = { [a : b]: c }
5787// Message: There must be a space before type annotation colon.
5788
5789// Options: ["never"]
5790type X = { [a: b] : c }
5791// Message: There must be no space before type annotation colon.
5792
5793// Options: ["always"]
5794type X = { [a : b] : c }
5795// Message: There must be 1 space before type annotation colon.
5796
5797// Options: ["always"]
5798type X = { [a:b]:c }
5799// Message: There must be a space before type annotation colon.
5800// Message: There must be a space before type annotation colon.
5801
5802// Options: ["never"]
5803type X = { [a : b] : c }
5804// Message: There must be no space before type annotation colon.
5805// Message: There must be no space before type annotation colon.
5806
5807// Options: ["always"]
5808type X = { [a : b] : c }
5809// Message: There must be 1 space before type annotation colon.
5810// Message: There must be 1 space before type annotation colon.
5811
5812// Options: ["always"]
5813type X = { [a:(b)]:(c) }
5814// Message: There must be a space before type annotation colon.
5815// Message: There must be a space before type annotation colon.
5816
5817// Options: ["never"]
5818type X = { [a : (b)] : (c) }
5819// Message: There must be no space before type annotation colon.
5820// Message: There must be no space before type annotation colon.
5821
5822// Options: ["never"]
5823const x = ({} :{})
5824// Message: There must be no space before type cast colon.
5825
5826// Options: ["always"]
5827const x = ({}:{})
5828// Message: There must be a space before type cast colon.
5829
5830// Options: ["always"]
5831const x = ({} :{})
5832// Message: There must be 1 space before type cast colon.
5833
5834// Options: ["never"]
5835((x) : string)
5836// Message: There must be no space before type cast colon.
5837
5838// Options: ["always"]
5839((x): string)
5840// Message: There must be a space before type cast colon.
5841
5842// Options: ["always"]
5843((x) : string)
5844// Message: There must be 1 space before type cast colon.
5845
5846// Options: ["always"]
5847const x:number = 7;
5848// Message: There must be a space before const type annotation colon.
5849
5850// Options: ["always"]
5851let x:number = 42;
5852// Message: There must be a space before let type annotation colon.
5853
5854// Options: ["always"]
5855var x:number = 42;
5856// Message: There must be a space before var type annotation colon.
5857```
5858
5859The following patterns are not considered problems:
5860
5861```js
5862(foo) => {}
5863
5864(foo: string) => {}
5865
5866(foo?: string) => {}
5867
5868(foo ?: string) => {}
5869
5870// Options: ["never"]
5871(foo: string) => {}
5872
5873// Options: ["always"]
5874(foo : string) => {}
5875
5876// Options: ["always"]
5877(foo? : string) => {}
5878
5879// Options: ["always"]
5880(foo ? : string) => {}
5881
5882// Options: ["always"]
5883(foo ? : string) => {}
5884
5885({ lorem, ipsum, dolor }: SomeType) => {}
5886
5887(foo: { a: string, b: number }) => {}
5888
5889({ a, b }: ?{ a: string, b: number }) => {}
5890
5891(): { a: number, b: string } => {}
5892
5893// Options: ["always"]
5894() : { a : number, b : string } => {}
5895
5896([ a, b ]: string[]) => {}
5897
5898(): x => {}
5899
5900// Options: ["always"]
5901() : x => {}
5902
5903(): (number | string) => {}
5904
5905// Options: ["always"]
5906() : (number | string) => {}
5907
5908function x(foo: string) {}
5909
5910// Options: ["always"]
5911function x(foo : string) {}
5912
5913var x = function (foo: string) {}
5914
5915// Options: ["always"]
5916var x = function (foo : string) {}
5917
5918class X { foo({ bar }: Props = this.props) {} }
5919
5920class Foo { constructor(foo: string ) {} }
5921
5922// Options: ["always"]
5923class Foo { constructor(foo : string ) {} }
5924
5925async function foo({ lorem, ipsum, dolor }: SomeType) {}
5926
5927function x({ a, b }: { a: string, b: number }) {}
5928
5929function a(): x {}
5930
5931// Options: ["always"]
5932function a() : x {}
5933
5934function a(): (number | string) {}
5935
5936// Options: ["always"]
5937function a() : (number | string) {}
5938
5939type X = (foo:string) => number;
5940
5941type X = (foo: string) => number;
5942
5943type X = (foo: ?string) => number;
5944
5945type X = (foo?: string) => number;
5946
5947type X = (foo?: ?string) => number;
5948
5949type X = (foo ?: string) => number;
5950
5951// Options: ["always"]
5952type X = (foo? : string) => number
5953
5954// Options: ["always"]
5955type X = (foo? : ?string) => number
5956
5957type X = (number) => string;
5958
5959type X = (?number) => string;
5960
5961type X = number => string;
5962
5963type X = ?number => string;
5964
5965type X = ({ foo: bar }) => string;
5966
5967// Options: ["always"]
5968type X = (number) => string;
5969
5970// Options: ["always"]
5971type X = (?number) => string;
5972
5973// Options: ["always"]
5974type X = number => string;
5975
5976// Options: ["always"]
5977type X = ?number => string;
5978
5979// Options: ["always"]
5980type X = ({ foo : bar }) => string;
5981
5982class Foo { bar }
5983
5984class Foo { bar = 3 }
5985
5986class Foo { bar: string }
5987
5988class Foo { bar: ?string }
5989
5990class Foo { bar:?string }
5991
5992// Options: ["always"]
5993class Foo { bar : string }
5994
5995class X { static foo:number }
5996
5997class X { static foo: number }
5998
5999// Options: ["always"]
6000class X { static foo :number }
6001
6002// Options: ["always"]
6003class X { static foo : number }
6004
6005declare class Foo { static bar:number; }
6006
6007// Options: ["always"]
6008declare class Foo { static bar :number; }
6009
6010declare class Foo { static bar: number; }
6011
6012// Options: ["always"]
6013declare class Foo { static bar : number; }
6014
6015class X { +foo: string }
6016
6017class X { static +foo: string }
6018
6019// Options: ["always"]
6020class X { +foo : string }
6021
6022// Options: ["always"]
6023class X { static +foo : string }
6024
6025type X = { foo: string }
6026
6027// Options: ["always"]
6028type X = { foo : string }
6029
6030type X = { foo?: string }
6031
6032type X = { foo ?: string }
6033
6034// Options: ["always"]
6035type X = { foo? : string }
6036
6037type X = { +foo: string }
6038
6039type X = { +foo?: string }
6040
6041// Options: ["always"]
6042type X = { +foo : string }
6043
6044// Options: ["always"]
6045type X = { +foo? : string }
6046
6047// Options: ["always"]
6048type X = { [a : b] : c }
6049
6050// Options: ["never"]
6051type X = { [a:b]:c }
6052
6053// Options: ["always"]
6054type X = { [string] : c }
6055
6056// Options: ["never"]
6057type X = { [string]:c }
6058
6059// Options: ["always"]
6060type X = { +[a : b] : c }
6061
6062// Options: ["never"]
6063type X = { +[a:b]:c }
6064
6065// Options: ["always"]
6066type X = { [a : (b)] : (c) }
6067
6068// Options: ["never"]
6069type X = { [a:(b)]:(c) }
6070
6071// Options: ["never"]
6072const x = ({}:{})
6073
6074// Options: ["always"]
6075const x = ({} :{})
6076
6077// Options: ["never"]
6078((x): string)
6079
6080// Options: ["always"]
6081((x) : string)
6082
6083// Options: ["always"]
6084const x :number = 7;
6085
6086// Options: ["always"]
6087let x :number = 42;
6088
6089// Options: ["always"]
6090var x :number = 42;
6091```
6092
6093
6094
6095<a name="eslint-plugin-flowtype-rules-spread-exact-type"></a>
6096### <code>spread-exact-type</code>
6097
6098Enforce object types, that are spread to be exact type explicitly.
6099
6100The following patterns are considered problems:
6101
6102```js
6103type bar = {...{test: string}}
6104// Message: Use $Exact to make type spreading safe.
6105
6106type foo = {test: number}; type bar = {...foo}
6107// Message: Use $Exact to make type spreading safe.
6108```
6109
6110The following patterns are not considered problems:
6111
6112```js
6113type bar = {...$Exact<{test: string}>}
6114
6115type foo = {test: number}; type bar = {...$Exact<foo>}
6116```
6117
6118
6119
6120<a name="eslint-plugin-flowtype-rules-type-id-match"></a>
6121### <code>type-id-match</code>
6122
6123Enforces a consistent naming pattern for type aliases.
6124
6125<a name="eslint-plugin-flowtype-rules-type-id-match-options-14"></a>
6126#### Options
6127
6128This rule requires a text RegExp:
6129
6130```js
6131{
6132 "rules": {
6133 "flowtype/type-id-match": [
6134 2,
6135 "^([A-Z][a-z0-9]*)+Type$"
6136 ]
6137 }
6138}
6139```
6140
6141`'^([A-Z][a-z0-9]*)+Type$$'` is the default pattern.
6142
6143The following patterns are considered problems:
6144
6145```js
6146opaque type foo = {};
6147// Message: Type identifier 'foo' does not match pattern '/^([A-Z][a-z0-9]*)+Type$/'.
6148
6149type foo = {};
6150// Message: Type identifier 'foo' does not match pattern '/^([A-Z][a-z0-9]*)+Type$/'.
6151
6152// Options: ["^foo$"]
6153type FooType = {};
6154// Message: Type identifier 'FooType' does not match pattern '/^foo$/'.
6155```
6156
6157The following patterns are not considered problems:
6158
6159```js
6160type FooType = {};
6161
6162// Options: ["^foo$"]
6163type foo = {};
6164
6165// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
6166type foo = {};
6167```
6168
6169
6170
6171<a name="eslint-plugin-flowtype-rules-type-import-style"></a>
6172### <code>type-import-style</code>
6173
6174_The `--fix` option on the command line automatically fixes problems reported by this rule._
6175
6176Enforces a particular style for type imports:
6177
6178```
6179// 'identifier' style
6180import {type T, type U, type V} from '...';
6181
6182// 'declaration' style
6183import type {T, U, V} from '...';
6184```
6185
6186<a name="eslint-plugin-flowtype-rules-type-import-style-options-15"></a>
6187#### Options
6188
6189The rule has a string option:
6190
6191* `"identifier"` (default): Enforces that type imports are all in the
6192 'identifier' style.
6193* `"declaration"`: Enforces that type imports are all in the 'declaration'
6194 style.
6195
6196This rule has an object option:
6197
6198* `ignoreTypeDefault` - if `true`, when in "identifier" mode, default type imports will be ignored. Default is `false`.
6199
6200The following patterns are considered problems:
6201
6202```js
6203import type {A, B} from 'a';
6204// Message: Unexpected "import type"
6205
6206// Options: ["identifier"]
6207import type {A, B} from 'a';
6208// Message: Unexpected "import type"
6209
6210// Options: ["identifier"]
6211import type {A, B as C} from 'a';
6212// Message: Unexpected "import type"
6213
6214// Options: ["identifier"]
6215import type A from 'a';
6216// Message: Unexpected "import type"
6217
6218// Options: ["declaration"]
6219import {type A, type B} from 'a';
6220// Message: Unexpected type import
6221// Message: Unexpected type import
6222```
6223
6224The following patterns are not considered problems:
6225
6226```js
6227import {type A, type B} from 'a';
6228
6229// Options: ["identifier"]
6230import {type A, type B} from 'a';
6231
6232// Options: ["declaration"]
6233import type {A, B} from 'a';
6234
6235// Options: ["identifier"]
6236import typeof * as A from 'a';
6237
6238// Options: ["identifier",{"ignoreTypeDefault":true}]
6239import type A from 'a';
6240
6241// Options: ["identifier"]
6242declare module "m" { import type A from 'a'; }
6243```
6244
6245
6246
6247<a name="eslint-plugin-flowtype-rules-union-intersection-spacing"></a>
6248### <code>union-intersection-spacing</code>
6249
6250_The `--fix` option on the command line automatically fixes problems reported by this rule._
6251
6252Enforces consistent spacing around union and intersection type separators (`|` and `&`).
6253
6254This rule takes one argument. If it is `'always'` then a problem is raised when there is no space around the separator. If it is `'never'` then a problem is raised when there is a space around the separator.
6255
6256The default value is `'always'`.
6257
6258The following patterns are considered problems:
6259
6260```js
6261type X = string| number;
6262// Message: There must be a space before union type annotation separator
6263
6264// Options: ["always"]
6265type X = string| number;
6266// Message: There must be a space before union type annotation separator
6267
6268type X = string |number;
6269// Message: There must be a space after union type annotation separator
6270
6271type X = string|number;
6272// Message: There must be a space before union type annotation separator
6273// Message: There must be a space after union type annotation separator
6274
6275type X = {x: string}|{y: number};
6276// Message: There must be a space before union type annotation separator
6277// Message: There must be a space after union type annotation separator
6278
6279type X = string | number |boolean;
6280// Message: There must be a space after union type annotation separator
6281
6282type X = string|number|boolean;
6283// Message: There must be a space before union type annotation separator
6284// Message: There must be a space after union type annotation separator
6285// Message: There must be a space before union type annotation separator
6286// Message: There must be a space after union type annotation separator
6287
6288type X = (string)| number;
6289// Message: There must be a space before union type annotation separator
6290
6291type X = ((string))|(number | foo);
6292// Message: There must be a space before union type annotation separator
6293// Message: There must be a space after union type annotation separator
6294
6295// Options: ["never"]
6296type X = string |number;
6297// Message: There must be no space before union type annotation separator
6298
6299// Options: ["never"]
6300type X = string| number;
6301// Message: There must be no space after union type annotation separator
6302
6303type X = string& number;
6304// Message: There must be a space before intersection type annotation separator
6305
6306// Options: ["always"]
6307type X = string& number;
6308// Message: There must be a space before intersection type annotation separator
6309
6310type X = string &number;
6311// Message: There must be a space after intersection type annotation separator
6312
6313type X = {x: string}&{y: number};
6314// Message: There must be a space before intersection type annotation separator
6315// Message: There must be a space after intersection type annotation separator
6316
6317type X = string&number;
6318// Message: There must be a space before intersection type annotation separator
6319// Message: There must be a space after intersection type annotation separator
6320
6321type X = string & number &boolean;
6322// Message: There must be a space after intersection type annotation separator
6323
6324type X = string&number&boolean;
6325// Message: There must be a space before intersection type annotation separator
6326// Message: There must be a space after intersection type annotation separator
6327// Message: There must be a space before intersection type annotation separator
6328// Message: There must be a space after intersection type annotation separator
6329
6330type X = (string)& number;
6331// Message: There must be a space before intersection type annotation separator
6332
6333type X = ((string))&(number & foo);
6334// Message: There must be a space before intersection type annotation separator
6335// Message: There must be a space after intersection type annotation separator
6336
6337// Options: ["never"]
6338type X = string &number;
6339// Message: There must be no space before intersection type annotation separator
6340
6341// Options: ["never"]
6342type X = string& number;
6343// Message: There must be no space after intersection type annotation separator
6344```
6345
6346The following patterns are not considered problems:
6347
6348```js
6349type X = string | number;
6350
6351type X = string | number | boolean;
6352
6353type X = (string) | number;
6354
6355type X = ((string)) | (number | foo);
6356
6357// Options: ["never"]
6358type X = string|number
6359
6360type X =
6361| string
6362| number
6363
6364function x() {
6365type X =
6366| string
6367| number
6368}
6369
6370// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
6371type X = string| number;
6372
6373type X = string & number;
6374
6375type X = string & number & boolean;
6376
6377type X = (string) & number;
6378
6379type X = ((string)) & (number & foo);
6380
6381// Options: ["never"]
6382type X = string&number
6383
6384type X =
6385& string
6386& number
6387
6388function x() {
6389type X =
6390& string
6391& number
6392}
6393
6394// Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
6395type X = string& number;
6396```
6397
6398
6399
6400<a name="eslint-plugin-flowtype-rules-use-flow-type"></a>
6401### <code>use-flow-type</code>
6402
6403Marks Flow [type alias](https://flowtype.org/docs/type-aliases.html) declarations as used.
6404
6405Used to suppress [`no-unused-vars`](http://eslint.org/docs/rules/no-unused-vars) errors that are triggered by type aliases.
6406
6407The following patterns are not considered problems:
6408
6409```js
6410declare class A {}
6411// Additional rules: {"no-unused-vars":1}
6412
6413declare function A(): Y
6414// Additional rules: {"no-unused-vars":1}
6415
6416declare module A {}
6417// Additional rules: {"no-unused-vars":1}
6418
6419declare module A { declare var a: Y }
6420// Additional rules: {"no-unused-vars":1}
6421
6422declare var A: Y
6423// Additional rules: {"no-unused-vars":1}
6424
6425import type A from "a"; type X<B = ComponentType<A>> = { b: B }; let x: X; console.log(x);
6426// Additional rules: {"no-unused-vars":1}
6427
6428import type A from "a"; type X<B = A<string>> = { b: B }; let x: X; console.log(x);
6429// Additional rules: {"no-unused-vars":1}
6430```
6431
6432
6433
6434<a name="eslint-plugin-flowtype-rules-use-read-only-spread"></a>
6435### <code>use-read-only-spread</code>
6436
6437Warns against accidentally creating an object which is no longer read-only because of how spread operator works in Flow. Imagine the following code:
6438
6439```flow js
6440type INode = {|
6441 +type: string,
6442|};
6443
6444type Identifier = {|
6445 ...INode,
6446 +name: string,
6447|};
6448```
6449
6450You might expect the identifier name to be read-only, however, that's not true ([flow.org/try](https://flow.org/try/#0C4TwDgpgBAkgcgewCbQLxQN4B8BQUoDUokAXFAM7ABOAlgHYDmANDlgL4DcOOx0MKdYDQBmNCFSjpseKADp58ZBBb4CdAIYBbCGUq1GLdlxwBjBHUpQAHmX4RBIsRKlQN2sgHIPTKL08eoTm4rWV5JKA8AZQALBABXABskVwRgKAAjaAB3WmB1dISIAEIPLhC3NAiY+KSUtMyoHJo8guLSnCA)):
6451
6452```flow js
6453const x: Identifier = { name: '', type: '' };
6454
6455x.type = 'Should not be writable!'; // No Flow error
6456x.name = 'Should not be writable!'; // No Flow error
6457```
6458
6459This rule suggests to use `$ReadOnly<…>` to prevent accidental loss of readonly-ness:
6460
6461```flow js
6462type Identifier = $ReadOnly<{|
6463 ...INode,
6464 +name: string,
6465|}>;
6466
6467const x: Identifier = { name: '', type: '' };
6468
6469x.type = 'Should not be writable!'; // $FlowExpectedError[cannot-write]
6470x.name = 'Should not be writable!'; // $FlowExpectedError[cannot-write]
6471```
6472
6473The following patterns are considered problems:
6474
6475```js
6476type INode = {||};
6477type Identifier = {|
6478 ...INode,
6479 +aaa: string,
6480|};
6481// Message: Flow type with spread property and all readonly properties should be wrapped in '$ReadOnly<…>' to prevent accidental loss of readonly-ness.
6482
6483type INode = {||};
6484type Identifier = {|
6485 ...INode,
6486 +aaa: string,
6487 +bbb: string,
6488|};
6489// Message: Flow type with spread property and all readonly properties should be wrapped in '$ReadOnly<…>' to prevent accidental loss of readonly-ness.
6490```
6491
6492The following patterns are not considered problems:
6493
6494```js
6495type INode = {||};
6496type Identifier = {|
6497 ...INode,
6498 name: string,
6499|};
6500
6501type INode = {||};
6502type Identifier = {|
6503 ...INode,
6504 name: string, // writable on purpose
6505 +surname: string,
6506|};
6507
6508type Identifier = {|
6509 +name: string,
6510|};
6511
6512type INode = {||};
6513type Identifier = $ReadOnly<{|
6514 ...INode,
6515 +name: string,
6516|}>;
6517
6518type INode = {||};
6519type Identifier = $ReadOnly<{|
6520 ...INode,
6521 name: string, // writable on purpose
6522|}>;
6523
6524type INode = {||};
6525type Identifier = $ReadOnly<{|
6526 ...INode,
6527 -name: string,
6528|}>;
6529```
6530
6531
6532
6533<a name="eslint-plugin-flowtype-rules-valid-syntax"></a>
6534### <code>valid-syntax</code>
6535
6536**Deprecated** Babylon (the Babel parser) v6.10.0 fixes parsing of the invalid syntax this plugin warned against.
6537
6538Checks for simple Flow syntax errors.
6539
6540The following patterns are not considered problems:
6541
6542```js
6543function x(foo: string = "1") {}
6544
6545function x(foo: Type = bar()) {}
6546```
6547
6548
6549