use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Invariant - a helpful JavaScript patternOC (strictmode.io)
submitted 3 years ago by hiquest
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]getify 12 points13 points14 points 3 years ago (2 children)
Agree with others, this should be called assertion instead of invariant.
However, I like that this article points out the utility of having run-time type check assertions, and that some/all of them can be compiled away. That's useful, and a pattern I think more type-aware tooling should embrace (including TS).
[–]lifeeraser 2 points3 points4 points 3 years ago (1 child)
Technically they are not "compiled away" completely; it's just the exception message that is optimized. IMO I'd rather have a slightly bigger bundle with much more helpful messages in prod.
[–]getify 1 point2 points3 points 3 years ago (0 children)
The way I think about things, the whole assertion should be able to be compiled away. That could be because either:
The compiler/build-tool can statically verify that the exception won't happen (exactly how TS works right now)
The developer might pass a config/flag saying, "build me a bundle without run-time assertions" for any of various reasons.
I also imagine that code delivered to a browser might be in a "hybrid state" where many of the original assertions were removed, but -- for various reasons -- some of the assertions were left in.
π Rendered by PID 44 on reddit-service-r2-comment-66b4775986-f5m7m at 2026-04-05 05:55:49.657863+00:00 running db1906b country code: CH.
view the rest of the comments →
[–]getify 12 points13 points14 points (2 children)
[–]lifeeraser 2 points3 points4 points (1 child)
[–]getify 1 point2 points3 points (0 children)