Search code examples
eslinttypescript-eslint

Is there any function's result value must be used specifier In Typescript?


I want to specify that the function's return value must be used using typescript. There is an attribute [[nodiscard]] in c++. Is there any similar attribute in Typescript?

Example:

function setSomeFields(someThing: MyClass) {
    const other = new MyClass();
    other.value = someThing.value;
    //...
    return other;
}
//...
// wanted: error or warning
setSomeFields(myClass);

// correct
const newMyClass = setSomeFields(myClass);

Solution

  • There was a discussion on eslint to for a no-unused-return-value rule that would give you the warning you want but a rule like that was never included. For now, the SonarJS plugin for eslint does seem to have such a rule.