1

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?

Nat Mote
  • 4,068
  • 18
  • 30
dhilt
  • 18,707
  • 8
  • 70
  • 85
  • The second argument of fetch can also specifically accept a `Headers` object which is much easier to type. That would force you to always use an instance of `Headers` and never a pojo. But I don't see that as a bad thing – Andrew Dec 02 '19 at 17:22

1 Answers1

2

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.

Nat Mote
  • 4,068
  • 18
  • 30