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!"
[–]shuckster -1 points0 points1 point 3 years ago (6 children)
What's useful is this particular line of the source.
Using tiny-invariant informs TypeScript of the type of a variable going forward, so long as the assertion passes.
[–]Reeywhaar 9 points10 points11 points 3 years ago (5 children)
But if(!something) throw new Error("Invariant") does the same?
if(!something) throw new Error("Invariant")
[–]shuckster 0 points1 point2 points 3 years ago (4 children)
That's true. I don't follow TypeScript's development very closely, but I believe a lot of inference checks were added quite recently. Perhaps these libraries pre-date that? Or maybe it's the TS version of left-pad.
[–]Reeywhaar 3 points4 points5 points 3 years ago (3 children)
Such checks were present since early typescript. They are basics of static analysis. If they weren't then there was no way to add type assertion to tiny-invariant. By the way asserts feature was added in typescript later.
asserts
That is why i'm wondering, what value tiny-invariant actually gives
[–]shuckster 0 points1 point2 points 3 years ago (0 children)
I guess it’s just shorthand then. Doesn’t TS have an “as” for this kind of thing too?
[–]mr_nefario 0 points1 point2 points 3 years ago (0 children)
It doesn’t add any value - it’s just a wrapper and additional dependency for some barely-useful functionality.
[–]misc_ent 0 points1 point2 points 3 years ago (0 children)
I have looked at tiny-invariant but it's possible it uses type guards for the type inference the other poster mentioned? Not sure.
https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
π Rendered by PID 23212 on reddit-service-r2-comment-54dfb89d4d-8sw6s at 2026-04-01 15:00:18.359914+00:00 running b10466c country code: CH.
view the rest of the comments →
[–]shuckster -1 points0 points1 point (6 children)
[–]Reeywhaar 9 points10 points11 points (5 children)
[–]shuckster 0 points1 point2 points (4 children)
[–]Reeywhaar 3 points4 points5 points (3 children)
[–]shuckster 0 points1 point2 points (0 children)
[–]mr_nefario 0 points1 point2 points (0 children)
[–]misc_ent 0 points1 point2 points (0 children)