JavaScript

JavaScript

By DeepSource

Prefer the use of $ReadOnlyArray instead of Array JS-0487
Type check

It is recommended to use $ReadOnlyArray instead of an Array or the array shorthand notation. $ReadOnlyArray is an immutable array collection type and the superclass of array and tuple types in Flow.

Prefer type annotations in all function parameters JS-0497
Type check

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.

Avoid the usage of primitive constructor types JS-0488
Type check

The use of primitive constructor types such as Boolean, Number and String is not preferred.

Avoid weak types JS-0491
Type check

It is recommended to avoid using weak type annotations any, Object, and Function. These types can cause the flow type checker to silently skip over portions of your code, which would have otherwise caused type errors. To make full usage and utilize the maximum benefits of type-checking, it is recommended to type each variable, function, object, etc., properly as suggested in the latter description.

Prefer having type alias for all union and intersection types JS-0493
Type check

It is recommended to make type aliases for all union and intersection types. If these are used in raw forms, it might be tempting to copy & paste them around the code. However, this brings source code pollution and unnecessary changes on several parts when these compound types need to be changed. Having a type alias also makes the type reusable without copy-pasting them. This works on the same principle of using a function in any programming language. We create a function that we think needs to be used more than once or make the code more functional and readable.