JavaScript

JavaScript

Made by DeepSource

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.

An exact object cant have extra properties in them. For example

// exact by default, no need to add `| |`
var city: { prop: string } = { prop: "Hello" }; // ok!
city = { prop: "Hello", prop2: "otherValue" }; // Error

In order to fix this, the developer has to specify that the object is not an exact object but an inexact object using the spread operator i.e ...

var city: { prop: string, ... } = { prop: "Hello" }; // ok!
city = { prop: "Hello", prop2: "otherValue" }; // ok

Bad Practice

type obj = {};

type obj = { prop1: string };

type obj = {};

type obj = { prop1: string };

Recommended

type obj = { obj: string, ... };

interface GeneralObj { obj: string }

declare class GeneralObj { obj: string }

type obj = {| |};

type obj = {| prop1: string |};

type obj = { [key: string]: string, ... };

type obj = number;