question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What are best practices for optional parameters?

See original GitHub issue

Consider this code. It’s a redux reducer, but you don’t need to know redux to help me with this issue. I was trying to follow this doc, and am running into problems.

// Types
// Types
type InitAction = { +type: typeof INIT | typeof INIT_SUCCESS };
type ErrorAction = { +type: typeof INIT_ERROR, +error: Error };
type Action = InitAction | ErrorAction;  // flow doesn't seem to be doing 

type InitState = {
  +error?: Error
};

export default function InitReducer(
  state: InitState = {},
  { type, error }: Action = {}  //  i need this param or jest complains
): InitState {
  switch (type) {
    case INIT_ERROR:
      Raven.captureException(error, { state }); // flow complains, error doesn't always exist
      return {
        ...state,
        error
      };
    case INIT_SUCCESS:
    case INIT:
    default:
      return state;
  }
}

Raven.captureException(error, { state }); expects error to be of type Error. Whenever the case is INIT_ERROR, error will always be defined. Is there a way to imply this in flow? Originally I thought the flow-types on the actions imply this, but flow doesn’t seem to like this. To fix it, I’ve gone with a more-generic Action type.

type Action = {
  +type: string,
  +error?: Error
};

however even with this Action-type i still need this line

error && Raven.captureException(error, { state });   // flow is now happy, but this seems wrong

I thought maybe flow would be smart enough to know when INIT_ERROR is the type, an error will always exist. Another way I’ve found to get flow to stop complaining, change Raven.captureException to allow Error | void, but this seems wrong too.

Am i doing something completely wrong? Did I find a bug? What should I do?

Apologies in advance - I’m very new to flow

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mackbrownecommented, Dec 8, 2017

@GAntoine great idea, however part of my question relates to whether the flow-typed definitions for raven.js should be changed. But thank you, I’ll post the question there

0reactions
mackbrownecommented, Dec 8, 2017

@GAntoine - Thank you. When i get an answer on any channel I’ve posted on, I’ll be sure to report back here and close myself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optional Parameters, Good or Bad? - Stack Overflow
I'll start by prefacing my answer by saying Any language feature can be used well or it can be used poorly. Optional parameters...
Read more >
Optional Parameters in Java: Common Strategies and ...
In this article, we'll explore some strategies for dealing with optional parameters in Java. We'll look at the strengths and weaknesses of ...
Read more >
Solved: Best practice for dealing with Optional Parameters...
Let's say we have just two parameters defined in the code; and in the script tool we have configured parameter 1 to be...
Read more >
Dealing with Optional Parameters in Go | by Peter Malina
Using Multiple Initialization Functions​​ The simplest solution for dealing with a small number of variables is to use different functions. This technique uses ......
Read more >
Named and Optional Arguments - C# Programming Guide
Named arguments free you from matching the order of arguments to the order of parameters in the parameter lists of called methods. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found