A value is stored in a variable only to be returned or thrown right after

tx0/ts:return-of-fresh-variablestrictwarningstyle

What it finds

const result = compute(); return result; gives a name to a value only to hand it on in the next statement, which adds a line and a name without saying more than return compute();.

The variable must be declared with const or let in the statement right before the return or throw, as the only variable of that declaration, without a type annotation, and must be used nowhere else. The fix writes the value into the return or throw; it is unsafe, since in TypeScript the return type of the function then types the value, which can change what is inferred for it.

From the rule's source, return_of_fresh_variable.rs.

Example from the rule's tests
function compute(a: number) {  const result = a * 2;  return result;tx0 reports: result is returned right after it is declared}function fail(message: string) {  const error = new Error(message);  throw error;tx0 reports: error is thrown right after it is declared}function typed(a: number) {  const result: number = a * 2;  return result;}function used(a: number) {  const result = a * 2;  console.log(result);  return result;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:return-of-fresh-variable" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:return-of-fresh-variable] reason="..."

tx0 explain tx0/ts:return-of-fresh-variable shows the same in the terminal. Found a false positive? Report it.