JavaScript

JavaScript

Made by DeepSource

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.

Here are a few recommended replacement approaches for these :

  • Instead of Function, properly type each argument and the return type of the function.
  • Instead of Object, use the exact object types or objects as maps or interfaces or even sealed and unsealed objects.
  • Using any is kind of ignoring type checking. It is unsafe, and it should be compeltly avoided. Instead of using any, prefer either generic, union, intersection or mixed wherever possible.

Bad Practice

function func(thing): any {}

function func(thing): Promise<any> {}

function func(thing): Promise<Promise<any>> {}

function func(thing): Object {}

function func(thing): Promise<Object> {}

function func(thing): Function {}

(func: any) => {}

Recommended

function func(thing): string {}

function func(thing): Promise<string> {}

function func(thing): Promise<Promise<string>> {}

(func?: string) => {}

(func: ?string) => {}

References