dhilt
dhilt

Reputation: 20844

Flow no-weak-types warning: optional argument of weak type with default value

Having this Flow'ed code

export default (resource: string, init?: Object = {}) =>

I'm getting following warning

warning Unexpected use of weak type "Object" flowtype/no-weak-types

The thing is the init argument is exactly native' fetch second argument (see MDN) which is optional and which is an object that might have more than 10 complicated properties, so I don't want to specify its type rather than generic "Object" or "any".

What is the right way to avoid this warning in my situation?

Upvotes: 1

Views: 558

Answers (1)

Nat Mote
Nat Mote

Reputation: 4086

The warning you are seeing is from ESLint, specifically the eslint-plugin-flowtype plugin, and not from Flow itself.

However, it's warning you about something valid. Object is an unsafe type in Flow. It is equivalent to any (it used to have some subtle differences, but no longer does). In general it is probably worthwhile to spell out the properties explicitly, even if they are complicated, with something like this:

type Init = {
  someProp: string,
  someOtherProp: number;
}

export default (resource: string, init?: Init = {}) => {}

(try)

You may also want to consider the mixed type, which is safe but inconvenient to use.

If you are sure that you want to accept the unsafe behavior of Object or any, you can turn off the ESLint rule for that line.

Upvotes: 2

Related Questions