Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set showHelp return type to never #213

Merged
merged 1 commit into from
Jun 21, 2022
Merged

Set showHelp return type to never #213

merged 1 commit into from
Jun 21, 2022

Conversation

ryanrhee
Copy link
Contributor

@ryanrhee ryanrhee commented Jun 21, 2022

Since showHelp() calls into process.exit(), they should use the same return type.

The return type of process.exit() is never, not void. This tells the typescript compiler that code branches that have showHelp() terminate, and that any code written as executing after showHelp() should be considered dead code (similar to code after a return statement).

Since `showHelp()` calls into `process.exit()`, they should use the same return type.
The return type of `process.exit()` is `never`, not `void`. This tells the typescript compiler that code branches that have `showHelp()` terminate, and that any code written as executing after `showHelp()` should be considered dead code (similar to code after a `return` statement).
@ryanrhee
Copy link
Contributor Author

ryanrhee commented Jun 21, 2022

As a workaround:

  const foo: string | undefined = "foo"

  function showHelp(): never {
    cli.showHelp()
    throw new Error("This is unreachable code")
  }
  
  if (foo === undefined) {
    showHelp()
  }
  
  // type of foo is now `string`

@sindresorhus sindresorhus merged commit db55316 into sindresorhus:main Jun 21, 2022
@ryanrhee ryanrhee deleted the patch-1 branch June 22, 2022 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants