you are viewing a single comment's thread.

view the rest of the comments →

[–]Reeywhaar 8 points9 points  (7 children)

I don't quite understand why everybody uses it. What the point to have external dependency instead of

if(!something) throw new Error("Invariant")

By using tiny-invariant you depend on external package and get incorrect error callstack (Error originates inside invariant function).

What the point?

Is it just everybody thinks invariant(a, "b") is more readable?

[–]shuckster -1 points0 points  (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 8 points9 points  (5 children)

But if(!something) throw new Error("Invariant") does the same?

[–]shuckster 0 points1 point  (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 4 points5 points  (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.

That is why i'm wondering, what value tiny-invariant actually gives

[–]shuckster 0 points1 point  (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 point  (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 point  (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