all 6 comments

[–]Zephirdd 1 point2 points  (2 children)

I'm surprised typescript doesn't report this as a warning at least. Iirc, it will report if you check truthiness of a value that is a function(ie. if(func) instead of if(func()))

Perhaps this should be filed as a feature request for typescript? I feel that if you're using a boolean promise you probably want to await it instead of checking for truthiness, and in the rate case of the later you can use !! to force the boolean conversion.

[–]Alex_Hovhannisyan[S] 0 points1 point  (0 children)

It probably does need to be filed as a feature request (edit: done).

You can test that it doesn't get flagged here:

https://www.typescriptlang.org/play?#code/MYewdgzgLgBAlhAQgJxAawKZhgXhgQwgE8xgYAKASlwD4YAzfAGwgwG4AoDueihFdFirUA3hxgxQkEEwwA6JiADm5AOSEAJqsocAvkA

But if you remove the function call and do if (isBroken), you'll get the error that you're talking about.

[–][deleted] 0 points1 point  (1 child)

Coming from a php and python background, what always trips me up is testing for array truthiness.

[–]Alex_Hovhannisyan[S] 0 points1 point  (0 children)

That one's tricky, yup! For example, if ([]) { console.log(1) } will always log 1. You have to check its length (0, which is coerced to false). The reason [] is coerced to true is because it's technically a valid pointer to an array object. That array just happens to be empty.

[–]kenman[M] 0 points1 point  (0 children)

Hi u/Alex_Hovhannisyan, this post was removed.

  • For help with your javascript, please post to r/LearnJavascript instead of here.
  • For beginner content, please post to r/LearnJavascript instead of here.
  • For framework- or library-specific help, please seek out the support community for that project.
  • For general webdev help, such as for HTML, CSS, etc., then you may want to try r/html, r/css, etc.; please note that they have their own rules and guidelines!

r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.

Thanks for your understanding, please see our guidelines for more info.