0

I have noticed in my debugging that the dopey typos which are hardest for me to find are as the result statements like:

if (id = userId) {..}

And for class methods:

let result = myClass.doThis;

For some reason VSCode doesn't flag these as potential problems at compile time. Is there a setting or a tool that would warn me about these sort of errors? Sometimes they are quite tedious to find and cost me quite a lot of time to track down because they aren't so obvious when you scanning and hard to search for.

I realize these are legal typescript statements but UIs in some other languages flag assignments inside conditionals and and methods called as properties to make sure that's really what you want to do.

4
  • 1
    Use a linter. eslint with typescript plugins is the way going forward as tslint is now depreciated. They'll catch the if situation. Your second example however is perfectly valid and sensible code so I don't see any reason to flag it. If a class method doesn't take any arguments, and is synchronous, consider making it a property accessor Commented Jul 19, 2020 at 23:37
  • I wonder how useful it ever is to save a class method inside a variable as in let result=myClass.doThis; When would that be useful? If "doThis" is a method which depends on any of the other properties of the class, then calling result() won't do much good without the proper 'this' context of the instance. Commented Jul 20, 2020 at 19:52
  • Well, sometimes you bind it and pass along or you're creating some kind adapter, but consider the more general case of object.member where object is just some bag of properties. I almost never use this except when frameworks compel me. Anyway, linters these days are highly customizable and I'm sure someone's written a rule like that already, but I thought I would give you my opinion in addition to recommending a tool because well... we are talking about linters :} Commented Jul 21, 2020 at 1:21
  • Thanks. Having trouble actually getting ESLint to work, but I'll keep fiddling. Commented Jul 21, 2020 at 15:46

1 Answer 1

1

Following on from @aluan-haddad's comments.

ESLint has a rule to prevent your first issue (if (id = userId) {...}): no-cond-assign.

Your second issue (let result = myClass.doThis;) it's hard to say the best lint rule to catch this without more information. By itself there's nothing wrong with that code - you've assigned (what I assume is) a function to a variable.

One option to catch this might be the unbound-method rule from @typescript-eslint. However, this rule will not catch anything if you're using auto-bound arrow-function methods.

If you have an issue wherein you're returning the value from a function, then TS should mostly handle this for you. One way to be sure is to enforce strict contracts for your functions via one of these @typescript-eslint rules:

If you're having issues with things like if (myClass.doThis) {...}, then instead see suggestions in this answer: https://stackoverflow.com/a/63183129/3736051

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.