News for hackers. Stuff that matters.
Re:Show HN: A SQL database implemented purely in TypeScript ... by Merad on Monday September 28, 2020 @09:01AM (#24616160)
Can't speak for the GP but my single biggest complaint about TS is how it's basically a massive set of assumptions layered on top of JS. If the JS types at runtime don't align with your assumptions in TS, the whole house of cards can come crashing down.
Re:Show HN: A SQL database implemented purely in TypeScript ... by smichel17 on Monday September 28, 2020 @09:28AM (#24616398)
This is why you need input validation. Eg, zod or io-ts.
Re:Show HN: A SQL database implemented purely in TypeScript ... by cjdell on Monday September 28, 2020 @09:36AM (#24616452)
Did not know about these. I wrote my own a while back because I couldn't find an example of this being done (fully type safe validation).

https://github.com/cjdell/type-safe-validator

Re:Show HN: A SQL database implemented purely in TypeScript ... by buu700 on Monday September 28, 2020 @09:52AM (#24616578)
Not exactly the same thing, but protobuf.js also works really well for this: https://github.com/protobufjs/protobuf.js
Re:Show HN: A SQL database implemented purely in TypeScript ... by 52-6F-62 on Monday September 28, 2020 @09:43AM (#24616506)
Which is the nature of the compiler. It maintains the promise that what you're writing is still JavaScript—in that you could just strip all the type annotations and have valid JS.

But I would love runtime checks for types. Even if that was a compiler step I could flag on—but that makes it a great deal more complex both to implement and reason about, likely—without starting fresh anyway.

Re:Show HN: A SQL database implemented purely in TypeScript ... by phpnode on Monday September 28, 2020 @10:31AM (#24616977)
A while ago I built this exact mechanism for Flow, it could be done in TypeScript if there was enough demand for it (it's rather a large effort) https://gajus.github.io/flow-runtime/
Re:Show HN: A SQL database implemented purely in TypeScript ... by mypalmike on Monday September 28, 2020 @02:58PM (#24620101)
Why do you need runtime type checks if the compiler already checked the type?

IMO, runtime type analysis is a code smell. There are few scenarios where the simpler and more efficient solution involves querying type metadata at runtime. Particularly in a language that directly supports dynamic dispatch, you should think hard before adding anything resembling an "if typeof(mything)" check.

Re:Show HN: A SQL database implemented purely in TypeScript ... by wolfgang42 on Monday September 28, 2020 @03:14PM (#24620247)
The obvious use case is the interface between something that’s not type-checked by the compiler and something that is. It’d be useful to be able to do `runtime_cast<Foo>(JSON.parse(foo_json))` on, say, the response to an API call and know that the result was definitely a valid Foo without having to write a bunch of boilerplate to validate that by hand.

(Reading through the thread, there’s apparently some libraries that can do this; I’ll have to try them out next time I need this.)

Re:Show HN: A SQL database implemented purely in TypeScript ... by int_19h on Monday September 28, 2020 @08:30PM (#24622834)
Because there's no guarantee that compiler did that. Even if you do this in your code, the moment it gets called from pure JS, all bets are off. With proper runtime checks, you could get consistent behavior out of that.
Re:Show HN: A SQL database implemented purely in TypeScript ... by vorticalbox on Monday September 28, 2020 @11:12AM (#24617489)
I was thinking about this the other day i would love for TS flag that would turn an interface into an assert

const shout = (message: string) => console.log(message)

into something like

const assert = require('assert'); const shout = (message) => { assert(typeof message === "string"); console.log(message); }

of course this doesn't work for more complicated types

Re:Show HN: A SQL database implemented purely in TypeScript ... by IggleSniggle on Monday September 28, 2020 @12:38PM (#24618497)
You should checkout myzod, which is very close to this experience.
Re:Show HN: A SQL database implemented purely in TypeScript ... by vorticalbox on Friday October 02, 2020 @03:05AM (#24659815)
Thanks for this
Re:Show HN: A SQL database implemented purely in TypeScript ... by colinmcd on Monday September 28, 2020 @06:01PM (#24621690)
Or just plain zod
Re:Show HN: A SQL database implemented purely in TypeScript ... by IggleSniggle on Wednesday September 30, 2020 @12:22AM (#24635675)
Genuinely curious, why would you pick zod over myzod?
Re:Show HN: A SQL database implemented purely in TypeScript ... by DougBTX on Monday September 28, 2020 @09:50AM (#24616562)
At the end of the day, if you look at a low enough level in the stack, there are no types. In a very concrete way, types are a high-level idea, and if the high-level stops matching the low-level, then bugs crop up.

See for example Tetris implemented in Pokémon Yellow via runtime code injection:

https://www.youtube.com/watch?v=Vjm8P8utT5g

Their compiler missed that!

So I think there's good reason to switch from talking about types as "assumptions", and think of them as compile time "assertions". Something lower-level in the stack could break the high-level code, but that doesn't make the high-level code useless, just imperfect.

Re:Show HN: A SQL database implemented purely in TypeScript ... by cjdell on Monday September 28, 2020 @09:58AM (#24616629)
I get what you're saying however even at the machine code level there is a difference between an i32 and an i64. Also the "sizeof" of a struct is important too when calculating how far to stride with regard to memory addresses.

Types can and do affect the way programs run at the lowest level. Perhaps someone with a better understanding of compiler theory can elaborate further on this...