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

Implement no discard error for return values #29173

Closed
4 of 5 tasks
abdulkareemnalband opened this issue Dec 27, 2018 · 3 comments
Closed
4 of 5 tasks

Implement no discard error for return values #29173

abdulkareemnalband opened this issue Dec 27, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@abdulkareemnalband
Copy link

abdulkareemnalband commented Dec 27, 2018

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.
@ajafff
Copy link
Contributor

ajafff commented Dec 27, 2018

Duplicate of #8240

@abdulkareemnalband
Copy link
Author

@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

@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