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

mustUse / noignore return values #38303

Closed
5 tasks done
wojpawlik opened this issue May 2, 2020 · 4 comments
Closed
5 tasks done

mustUse / noignore return values #38303

wojpawlik opened this issue May 2, 2020 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@wojpawlik
Copy link

Search Terms

no ignore noignore must use mustUse

Suggestion

declare function returnsPromise(): noignore Promise<void>

Not using the returned value would be an error. Value can be used by:

  1. Passing it to a function or method,
  2. Calling its method,
  3. Using one of its properties,
  4. awaiting it, if it's a Promise,
  5. yielding it,
  6. Returning it.
    Inferred return type should be mustUse / noignore.

Use Cases

  1. Detecting unhandled Promise rejections (related: Suggestion: throws clause and typed catch clause #13219),
  2. Detecting unnecessary calls to pure functions (related: Add pure and immutable keywords to ensure code has no unintended side-effects #17181).

Examples

declare function returnsPromise(): noignore Promise<void>

foo(returnsPromise()) // ok (1)
returnsPromise().catch(...) // ok (2)
await returnsPromise() // ok (4)
return returnsPromise() // ok (6)
returnsPromise() // error

array.splice(0, 1) // ok
bar(array.slice(1)) // ok (1)
array.slice(1).forEach(...) // ok (2)
array.slice(1).length // error, `.length` unused
baz(array.slice(1).length) // weird but ok (3, 1)
return array.slice(1).length // weird but ok (3, 6)
yield array.slice(1) // ok (5)
yield* array.slice(1) // ok (5)
return array.slice(1) // ok (6)
array.slice(1) // error

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@ikokostya
Copy link
Contributor

ikokostya commented May 3, 2020

In most cases unused return value should be considered as error or warning. For this reason make sense in opposite approach when we can specify that return value should be ignored:

returnsPromise(); // must be error or warning, because return value is ignored
_ = returnsPromise(); // OK

See for example https://docs.microsoft.com/en-us/dotnet/csharp/discards

@ikokostya
Copy link
Contributor

Also related to #13376

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 5, 2020
@RyanCavanaugh
Copy link
Member

I don't think this covers any ground not already covered by existing issues.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants