Skip to content

mustUse / noignore return values #38303

Closed

Description

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.

Activity

  1. ikokostya commented on May 3, 2020

    @ikokostya
    Contributor

    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

  2. ikokostya commented on May 3, 2020

    @ikokostya
    Contributor

    Also related to #13376

  3. RyanCavanaugh commented on May 5, 2020

    @RyanCavanaugh
    Member

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

  4. typescript-bot commented on May 7, 2020

    @typescript-bot
    Contributor

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

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions