Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
eslint-plugin-flowtype
Advanced tools
eslint-plugin-flowtype is an ESLint plugin that provides linting rules for Flow type annotations. It helps ensure that your Flow type annotations are correct and consistent, improving the reliability and maintainability of your code.
Type Annotations
This rule ensures that every file has a valid Flow file annotation. The code sample demonstrates a simple function with Flow type annotations for the parameters and return type.
/* eslint flowtype/require-valid-file-annotation: [2, "always"] */
// @flow
function add(a: number, b: number): number {
return a + b;
}
Type Definitions
This rule ensures that all Flow type definitions are defined correctly. The code sample shows a type definition for a User object and an instance of that type.
/* eslint flowtype/define-flow-type: 2 */
// @flow
type User = {
name: string,
age: number
};
const user: User = { name: 'John', age: 30 };
Type Casting
This rule enforces a naming convention for type identifiers. The code sample demonstrates a type definition that follows the specified naming convention.
/* eslint flowtype/type-id-match: [2, "^([A-Z][a-z0-9]+)+Type$"] */
// @flow
type UserType = {
name: string,
age: number
};
const user: UserType = { name: 'John', age: 30 };
eslint-plugin-typescript is an ESLint plugin that provides linting rules for TypeScript code. It offers similar functionality to eslint-plugin-flowtype but is designed for TypeScript instead of Flow. It helps ensure that your TypeScript code is correct and consistent.
typescript-eslint is a set of tools that allows ESLint to lint TypeScript code. It includes an ESLint plugin and a parser that can handle TypeScript syntax. Like eslint-plugin-flowtype, it helps maintain code quality and consistency, but it is tailored for TypeScript.
Flow type linting rules for ESLint.
babel-eslint
parser (ESLint parser does not support type annotations).eslint-plugin-flowtype
plugin.npm install eslint
npm install babel-eslint
npm install eslint-plugin-flowtype
parser
property to babel-eslint
.plugins
section and specify eslint-plugin-flowtype
as a plugin.{
"parser": "babel-eslint",
"plugins": [
"flowtype"
],
"rules": {
"flowtype/require-parameter-type": 1,
"flowtype/require-return-type": [
1,
"always",
{
"annotateUndefined": "never"
}
],
"flowtype/space-after-type-colon": [
1,
"always"
],
"flowtype/space-before-type-colon": [
1,
"never"
],
"flowtype/type-id-match": [
1,
"^([A-Z][a-z0-9]+)+Type$"
]
}
}
require-parameter-type
Requires that all function parameters have type annotations.
The following patterns are considered problems:
(foo) => {}
// Message: Missing "foo" parameter type annotation.
(foo = 'FOO') => {}
// Message: Missing "foo" parameter type annotation.
(...foo) => {}
// Message: Missing "foo" parameter type annotation.
({foo}) => {}
// Message: Missing "{foo}" parameter type annotation.
([foo]) => {}
// Message: Missing "[foo]" parameter type annotation.
The following patterns are not considered problems:
(foo: string) => {}
(foo: string = 'FOO') => {}
(...foo: string) => {}
({foo}: {foo: string}) => {}
([foo]: Array) => {}
require-return-type
Requires that functions have return type annotation.
The following patterns are considered problems:
(foo) => { return "foo"; }
// Message: Missing return type annotation.
// Options: ["always"]
(foo) => { return "foo"; }
// Message: Missing return type annotation.
(foo): undefined => { return; }
// Message: Must not annotate undefined return type.
(foo): undefined => { return undefined; }
// Message: Must not annotate undefined return type.
// Options: ["always",{"annotateUndefined":"never"}]
(foo): undefined => { return; }
// Message: Must not annotate undefined return type.
// Options: ["always",{"annotateUndefined":"always"}]
(foo) => { return; }
// Message: Must annotate undefined return type.
// Options: ["always",{"annotateUndefined":"never"}]
(foo): undefined => { return undefined; }
// Message: Must not annotate undefined return type.
// Options: ["always",{"annotateUndefined":"always"}]
(foo) => { return undefined; }
// Message: Must annotate undefined return type.
The following patterns are not considered problems:
(foo): string => {}
// Options: ["always"]
(foo): string => {}
(foo) => { return; }
(foo) => { return undefined; }
// Options: ["always",{"annotateUndefined":"always"}]
(foo): undefined => { return; }
// Options: ["always",{"annotateUndefined":"never"}]
(foo) => { return; }
// Options: ["always",{"annotateUndefined":"never"}]
(foo) => { return undefined; }
// Options: ["always",{"annotateUndefined":"always"}]
(foo): undefined => { return undefined; }
space-after-type-colon
Enforces consistent spacing after the type annotation colon.
This rule takes one argument. If it is 'always'
then a problem is raised when there is no space after the type annotation colon. If it is 'never'
then a problem is raised when there is a space after the type annotation colon. The default value is 'always'
.
The following patterns are considered problems:
// Options: ["never"]
(foo: string) => {}
// Message: There must be no space after "foo" parameter type annotation colon.
// Options: ["never"]
export default function (foo: string) {}
// Message: There must be no space after "foo" parameter type annotation colon.
// Options: ["never"]
function foo (foo: string) {}
// Message: There must be no space after "foo" parameter type annotation colon.
// Options: ["always"]
(foo:string) => {}
// Message: There must be a space after "foo" parameter type annotation colon.
// Options: ["always"]
(foo: string) => {}
// Message: There must be 1 space after "foo" parameter type annotation colon.
The following patterns are not considered problems:
(foo) => {}
(foo: string) => {}
// Options: ["never"]
(foo:string) => {}
// Options: ["always"]
(foo: string) => {}
space-before-type-colon
Enforces consistent spacing before the type annotation colon.
This 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'
.
The following patterns are considered problems:
// Options: ["never"]
(foo : string) => {}
// Message: There must be no space before "foo" parameter type annotation colon.
// Options: ["always"]
(foo: string) => {}
// Message: There must be a space before "foo" parameter type annotation colon.
// Options: ["always"]
(foo : string) => {}
// Message: There must be 1 space before "foo" parameter type annotation colon.
The following patterns are not considered problems:
(foo) => {}
(foo: string) => {}
// Options: ["never"]
(foo: string) => {}
// Options: ["always"]
(foo : string) => {}
type-id-match
Enforces a consistent naming pattern for type aliases.
This rule needs a text RegExp to operate with Its signature is as follows:
{
"rules": {
"flowtype/type-id-match": [
2,
"^([A-Z][a-z0-9]+)+Type$"
]
}
}
'^([A-Z][a-z0-9]+)+Type
is the default pattern.
The following patterns are considered problems:
type foo = {};
// Message: Type identifier 'foo' does not match pattern '/^([A-Z][a-z0-9]+)+Type$/'.
// Options: ["^foo$"]
type FooType = {};
// Message: Type identifier 'FooType' does not match pattern '/^foo$/'.
The following patterns are not considered problems:
type FooType = {};
// Options: ["^foo$"]
type foo = {};
FAQs
Flowtype linting rules for ESLint.
The npm package eslint-plugin-flowtype receives a total of 3,559,665 weekly downloads. As such, eslint-plugin-flowtype popularity was classified as popular.
We found that eslint-plugin-flowtype demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.