JavaScript

JavaScript

Made by DeepSource

Prefer type annotations in all function parameters JS-0497

Type check
Minor

All function parameters should have type annotations to get the benefits of type checking. Having proper types for parameters also helps detect wrong function calls with different arguments in the call statement or a different order. It is recommended not to use the Function type as they don't tell the proper or exact type of the function. Also prefer any or (...args: Array<any>) => any over Function as Function will be deprecated and removed in a future version of Flow.

When it is known the each parameter's type but not the number of parameters the function is using i.e., something like this

function func(...args) {
}

We can add a type for this using the rest parameters and array type.

function func(...args: Array<number>){
}

Bad Practice

(data) => {}

function x(data) {}

(data = 'Value') => {}

(...data) => {}

({data}) => {}

([data]) => {}

({data = 1} = {}) => {}

Recommended

(data: string) => {}

(data: string = 'NAME') => {}

(...data: string) => {}

const f: Data = (a, b) => 42;

({data}: {data: string}) => {}

([data]: Array) => {}

type fn = (a: string, b: number) => number;
const f: fn = (a, b) => {}

References