Skip to content

Instantly share code, notes, and snippets.

@makenova
Last active August 15, 2019 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makenova/3897c67d69f5cbe4022e8fb24e92f49d to your computer and use it in GitHub Desktop.
Save makenova/3897c67d69f5cbe4022e8fb24e92f49d to your computer and use it in GitHub Desktop.
a description of a flow default value gotcha

With in JS you can define default parameters and with flow you can type them. I wasn't careful and the following bug crept into production.

  const somefunc = ({ hasExpiredToken = false }: { hasExpiredToken: boolean }) => {
      doSomething({ hasExpiredToken, navigation });
  };

It is not obvious but when the above function is called without arguements it will cause an error because even though the propertes of the object have default values the object itself does not. So when the function is called, it will try to destructure hasExpiredToken from undefined which will fail.

I had to call it with an empty object i.e. somefunc({}) or define a default value for the function like so:

  const somefunc = ({ hasExpiredToken = false }: { hasExpiredToken: boolean } = {}) => {
      doSomething({ hasExpiredToken, navigation });
  };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment