You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
If a function returns error_code programmer always need to check for errors before proceeding to next step
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
functionsomeCalulation():ErrorCode[[nodiscard]]{}consterror=someCalculation();// good buildssomeCalculation();// compiler error return value discarded
Type
classVeryImportant[[nodiscard]]{}functionsomeCalulation():VeryImportant{}consterror=someCalculation();// good buildssomeCalculation();// 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.)
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/nodiscardNodiscard function/method
Annotate function/method with
nodiscard
then its return value should never be discardedNodiscard type
Annotate
class
,interface
,enum
,and/ortype
can be annotated withnodiscard
then , if any function returns value of that type ,then returned value should never be discardedwhat if user wants to discard the value any way
user can use same
ts-ignore
override compiler errorUse Cases
error_code
programmer always need to check for errors before proceeding to next stepObservable
programmer always need to subscribe that or need call that function asObservable
s are evaluated only on subscribe.Examples
This is purely conceptual (I have no idea which syntax will suit this)
Function/Method
Type
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: