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.

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

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

Avoid weak types JS-0491
Type check
Major

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
Minor

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.

Prefer using exact object types JS-0494
Type check
Minor
Autofix

It is recommended to use exact object types wherever possible as it improves type checking. It is considered safe to pass an object with extra properties where a normal object type is expected in Flow. So when we add type for an object, for example:

let obj: { prop: string } =  { prop : "someValue" }

This object obj can even be assigned with more extra properties like this.

obj =  { prop : "someValue",  prop2: "someOtherValule" } // fine, no typechecking errors

Flow wont throw any typechecking error here. This is because { prop : "someValue", prop2: "someOtherValule" } is subtype of { prop : "someValue" }. Here we can see that this is not a proper and accurate type checking as we might need to know what all properties/methods are present in an object. This is why exact object types are preferred as exact object types disable this subtyping and disallow extra properties.

let obj: { | prop: string | } =  { prop : "someValue" } // ok
obj =  { prop : "someValue",  prop2: "someOtherValule" } // Error.
Require inexact object types JS-0496
Type check
Minor

It is recommended to use explicit inexact object types. In a project using exact-by-default syntax, the explicit in-exact object type syntax is the only way to express an inexact object type. Otherwise, it would always consider any object type as exact object types. So the developer might be using an exact object type knowing it is an inexact object type, and if the developer adds any extra properties to it, flow will throw an error.

Intersection or union definition have duplicate types JS-T1000
Type check
Minor
Autofix

Having duplicate types in an intersection or union type definition impacts readability. It is often the result of a mistake while declaring the types.

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

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.

Indexers must be declared with key name JS-0495
Type check
Minor

Adding keys to the indexers is considered a good practice and is preferred because an object will likely have properties added to it and retrieved throughout its lifecycle. Furthermore, the property keys may not even be known statically, so writing out a type annotation would not be possible. Flow provides a special property for objects like these, called an "indexer property". An indexer property allows reads and writes using any key that matches the indexer key type.

Optional property in interface has an 'undefined' type JS-T1001
Type check
Minor
Autofix

Optional property syntax (?) implicitly adds an 'undefined' type to the property.

Explicitly having an 'undefined' type in the union of an optional property is redundant and should be avoided to improve the readability of the code.