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

Warn/error on pointless expression? #12576

Closed
donaldpipowitch opened this issue Nov 30, 2016 · 5 comments
Closed

Warn/error on pointless expression? #12576

donaldpipowitch opened this issue Nov 30, 2016 · 5 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@donaldpipowitch
Copy link
Contributor

donaldpipowitch commented Nov 30, 2016

This is a feature request.

This is an example to some similar code I recently encountered. It was a little bit more complex in the real code.

interface Foo {
  bar?: {
    baz?: {
      nope?: number;
    };
  };
}

const foo = {
  bar: {
    baz: {
      nope: 1    
    }
  }
}

if (foo.bar && foo.bar.baz && foo.bar.baz.nope === 1) {
  foo.bar.baz.nope === 2;
}

Can you see the problem on first glance? Probably. But with more noise and stress around me, I couldn't see the bug for a while. "Of course" foo.bar.baz.nope === 2; should be foo.bar.baz.nope === 2;. There is no point to have a line like foo.bar.baz.nope === 2;.

Maybe TypeScript could warn/error for cases like this with an additional setting (similar to settings like noUnusedLocals or noUnusedParameters). Maybe noUnusedExpressions? If expression is used without assigning the value to a variable or without being used inside if () and similar cases, I don't see why anyone would want to write an expression. If someone would write that to invoke some side effects that would be a bad style, too (IMHO).

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Nov 30, 2016

As a work-around, TSLint's no-unused-expressions rule handles this.

@donaldpipowitch
Copy link
Contributor Author

Nice. Thank you.

@yortus
Copy link
Contributor

yortus commented Nov 30, 2016

There is some related discussion of side-effect-free expressions in #10814.

@donaldpipowitch
Copy link
Contributor Author

Cool, thanks for the link.

@mhegazy mhegazy added Suggestion An idea for TypeScript Out of Scope This idea sits outside of the TypeScript language design constraints labels Dec 16, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 16, 2016

This falls more into the realm of a linter, and would be out of scope for the TS project at the time being.

@mhegazy mhegazy closed this as completed Dec 16, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants