Skip to content

Implement no discard error for return values #29173

Closed

Description

Search Terms

nodiscard , check return , must use, must inspect result , warn unused result , check return value

Suggestion

Allow marking functions who's return value should not be discarded, like c++ [[nodiscard]] attribute https://en.cppreference.com/w/cpp/language/attributes/nodiscard

Nodiscard function/method

Annotate function/method with nodiscard then its return value should never be discarded

Nodiscard type

Annotate class , interface ,enum ,and/or type can be annotated with nodiscard then , if any function returns value of that type ,then returned value should never be discarded

what if user wants to discard the value any way

user can use same ts-ignore override compiler error

Use Cases

  1. If a function returns error_code programmer always need to check for errors before proceeding to next step
  2. If a function returns Observable programmer always need to subscribe that or need call that function as Observables are evaluated only on subscribe.

Examples

This is purely conceptual (I have no idea which syntax will suit this)

Function/Method

function someCalulation():ErrorCode [[nodiscard]]{}
const error = someCalculation(); // good builds
someCalculation(); // compiler error return value discarded

Type

class VeryImportant [[nodiscard]]{}
function someCalulation():VeryImportant {}
const error = someCalculation(); // good builds
someCalculation(); // compiler error return value discarded

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code (it depends on syntax which need to be decided)
  • 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. ajafff commented on Dec 27, 2018

    @ajafff
    Contributor

    Duplicate of #8240

  2. abdulkareemnalband commented on Dec 29, 2018

    @abdulkareemnalband
    Author

    Klaus Meinhardt (@ajafff) #8240 is closed may be #8584 is more appropriate issue

    also I have edited description with more search terms which i failed to add before

  3. typescript-bot commented on Dec 31, 2018

    @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.

  4. 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