News for hackers. Stuff that matters.
Re:Show HN: A SQL database implemented purely in TypeScript ... by cjdell on Monday September 28, 2020 @08:40AM (#24615987)
TypeScript is becoming such a compelling language due to its insanely advanced type system (that allows for projects like this) that I now want to use it everywhere. I want it to become the next Python.

I know Deno is supposed to be first class TypeScript but under the hood it's still a JavaScript runtime with all the baggage that comes with that.

AssemblyScript is extremely interesting but last time I played with it I concluded it wasn't yet ready for anything serious, or is that no longer the case?

A first class TypeScript runtime with no JS overhead would be a dream come true. I just hope it happens someday, or that other type systems get these amazing features.

Re:Show HN: A SQL database implemented purely in TypeScript ... by erikpukinskis on Monday September 28, 2020 @11:57AM (#24618026)
I’ve only been writing TypeScript for about a year, so I might be getting something wrong, but here’s my understanding...

I’m not sure your request fully makes sense. TypeScript is extremely expressive. You can specify basically any type you what... stuff like “The Number 4 Or A Function That Returns The Number 4 Or An Object With Any Attribute Which Is Equal To 4” and TS will just work with that.

This SQL compiler is a perfect example of how far that can be taken. But I think it’s important to realize how this differs from a strongly typed, compiled language.

A language like Go doesn’t just need to know you’re passing an “array of structs with attributes x, y, and z” for fun. It actually uses that information to generate a binary. The types are not just a check that happens before the real compiling begins, they are how the compiler thinks about structuring the executable.

I would love if a compiler expert could chime in here, but my understanding is you could never write a Typescript compiler that was “strongly” typed because there is no data structure that can efficiently represent arbitrary types like “4 Or An Object With An Attribute Set To 4” in any meaningful way.

These kinds of “wacky types” are things you can statically analyze but not really “compile” per se. That’s why it’s a good fit for being transpiled down to a language with no types at all.

TL;DR, TypeScript is not really a “typed programming language”, it’s a static analysis tool. It’s not really designed to “run”.

Re:Show HN: A SQL database implemented purely in TypeScript ... by dwohnitmok on Monday September 28, 2020 @12:16PM (#24618260)
Types do not need to have a one-to-one correspondence with runtime data structures and indeed in many type systems often do not. In the limit, a type does not need to have any runtime representation at all (this is more than just generic erasure or phantom type parameters, you can have an entire type signature that has absolutely no runtime representation of any of its parts).

There is not really a fundamental difference between types and static analysis.

Re:Show HN: A SQL database implemented purely in TypeScript ... by jacinabox on Monday September 28, 2020 @07:40PM (#24622492)
A predicate is a computer program that accepts an input and returns "yes/no." A type system could be an arbitrary predicate which returns "well typed/not well typed" when given a term. But obviously there must be some difference between types and predicates, such as extensionality -- the type of an expression can be decided by examining the code alone.
Re:Show HN: A SQL database implemented purely in TypeScript ... by dwohnitmok on Tuesday September 29, 2020 @09:45AM (#24627716)
Are you responding to my assertion that there's no fundamental difference between type systems and static analysis? If so, if we just change "well typed/not well typed" to "acceptable/not acceptable" I think you've pretty much just described static analysis.
Re:Show HN: A SQL database implemented purely in TypeScript ... by dandare on Monday September 28, 2020 @05:26PM (#24621395)
> You can specify basically any type you what... stuff like “The Number 4 Or A Function That Returns The Number 4 Or An Object With Any Attribute Which Is Equal To 4” and TS will just work with that.

Hi, an amateur here. When I started to learn JS and later TS I always disliked this expressiveness. Like when the first parameter you pass to JQuery can be anything in the world: a DOM element, or collection of elements, or an object, or a function - and only then you declare what you want to do with it. Yes, the syntax is short. And you pay for it with a lousy code readability and steep learning curve.

My question is, is there a name for this "expressiveness"? Is there a name for the opposite? I am looking at the definition of AssemblyScript: "...compiles a strict variant of TypeScript (basically JavaScript with types)". But the word "type" is again used recursively here. Is there a name for this "strict type"?

Re:Show HN: A SQL database implemented purely in TypeScript ... by fendy3002 on Monday September 28, 2020 @07:35PM (#24622447)
> Like when the first parameter you pass to JQuery can be anything in the world: a DOM element, or collection of elements, or an object, or a function

That specific part is called overloading in OOP (java). Don't know about the rest.

Re:Show HN: A SQL database implemented purely in TypeScript ... by daeken on Monday September 28, 2020 @01:24PM (#24619049)
> I would love if a compiler expert could chime in here, but my understanding is you could never write a Typescript compiler that was “strongly” typed because there is no data structure that can efficiently represent arbitrary types like “4 Or An Object With An Attribute Set To 4” in any meaningful way.

(I don't consider myself a compiler expert by any means, but I've been doing it for nearly 20 years, so I feel relatively safe weighing in here.)

In short, if you were compiling a function like that for bare metal (or anything relatively like it), there would be no one function that takes in all of those types and acts on them. Instead, you would compile that function once for each combination of incoming types. The version that only takes the number 4 would not have any inputs at all and may end up entirely constant (depending on what it does, of course). It's then up to the caller to make sure the right variant is called. If you have strong types up the chain, this is easy and completely overhead-free -- they'll be compiled in their different versions, too. If you have a weaker type constraint (e.g. just any Number, rather than THE Number 4), then you'd do conditional dispatching at the call site.

For what it's worth, this is essentially how templates work in C++. It's also why compilation can take so damn long and can produce gigantic binaries, because when you have a function with 2 inputs of 4 different potential types each, you're up to 16 variants. Change that to 5 inputs of 4 different types and you're at 625 variants.

All that said, static compilation of TypeScript in its current form would be very difficult to make efficient, simply because JavaScript types in the abstract don't map nicely to hardware; Arrays can be extremely simple and linear or ungodly complex with holes and property overrides, and the 'right' thing to do with a Number is often different from the fast thing to do. That's why JITs are so valuable; they allow you to get around the ambiguity.

Edit to add: I should mention, it's possible you won't actually generate all combinations. You might know that certain combinations are impossible to hit due to other type constraints, or you might just know that they're unused (which leads to problems when you don't have the original function definitions to turn to; that's why C++ templates have to be in headers (I think that's true, at least? I might be wrong on that; I just use the magic, I don't understand it)).

Re:Show HN: A SQL database implemented purely in TypeScript ... by zensavona on Monday September 28, 2020 @12:18PM (#24618281)
I don't really know anything, but isn't this how Haskell and Elm work? With similarly expressive type systems?
Re:Show HN: A SQL database implemented purely in TypeScript ... by dwohnitmok on Monday September 28, 2020 @12:44PM (#24618561)
There's a general mismatch between popular conceptions of type system expressiveness and actual type system capabilities. Elm has a very simple type system. In fact it is very close to having among the simplest type systems in any statically typed programming language (the only things that really set it apart are tagged union types and record types). For example Java has a much more expressive type system than Elm in many ways. What perhaps makes Elm's type system seem advanced is that the runtime essentially never violates the guarantees of its type system, unlike Java (depending on your opinion of unchecked exceptions).

Haskell has a much more expressive type system than Elm, but still lags behind Typescript unless you turn on a truly gargantuan number of extensions.

It's also a bit weird to compare the languages because Typescript has a very different typing regimen than Haskell or Elm. Typescript is entirely structural while Haskell is almost entirely nominal and Elm is mostly nominal (with the exception of records).

Typescript has an extremely expressive type system. It is in fact so expressive I'm amazed that it's gotten so much adoption when languages like Haskell are still considered "advanced." I suspect it has to do with the semantics of the languages rather than the type systems.

Re:Show HN: A SQL database implemented purely in TypeScript ... by dllthomas on Monday September 28, 2020 @02:09PM (#24619569)
> Haskell has a much more expressive type system than Elm, but still lags behind Typescript unless you turn on a truly gargantuan number of extensions.

IIUC: it's not a total order, Haskell (even '98) has things TypeScript doesn't[0], and TypeScript has things that Haskell (even with extensions) doesn't[1].

[0]: eg. higher-kinded types [1]: eg. (convenient) row polymorphism

Re:Show HN: A SQL database implemented purely in TypeScript ... by dwohnitmok on Monday September 28, 2020 @02:52PM (#24620039)
Yeah you're right it's not a total order. I was being fast and loose and conveying a subjective feeling.

RE row polymorphism, Typescript doesn't quite have what users of ML-like languages are asking for when they want row polymorphism (which is usually parametric polymorphism rather than subtyping). But it's close.

As for convenient... well I'd argue by the time you've got the whole cornucopia of GHC extensions at the top of your file nothing is quite convenient at that point.

Re:Show HN: A SQL database implemented purely in TypeScript ... by dllthomas on Monday September 28, 2020 @03:59PM (#24620669)
> RE row polymorphism, Typescript doesn't quite have what users of ML-like languages are asking for when they want row polymorphism (which is usually parametric polymorphism rather than subtyping). But it's close.

How would you distinguish this from the following?

    function foo<T>(x: T & {field: number}): T {
        ...
    }
> As for convenient... well I'd argue by the time you've got the whole cornucopia of GHC extensions at the top of your file nothing is quite convenient at that point.

That was one part of my point. That said, I think the problems implied by language extensions are often exaggerated (probably not deliberately so, most of the time).

The other part of my point was that even with all the extensions you need, I've not found good row types in Haskell, although the particular failings vary by approach. Which isn't to say I can't get something that works well enough for my particular situation most of the time.

Re:Show HN: A SQL database implemented purely in TypeScript ... by dwohnitmok on Monday September 28, 2020 @04:31PM (#24620923)

    function foo<T>(x: T & {field: number}): T {
        return x;
    }
    
    function bar<T>(x: T & {field: number}): T {
        const x0 = foo(x);
        // type error because x0 doesn't have field
        // would compile fine with row types
        const x1 = foo(x0);
        return x1;
    }
Yes it's true. I also sorely miss the lack of row types in Haskell (I miss them even in something like Idris where you can create them more easily, but it's still not great compared to first-class support).

EDIT: I may be being dumb. You could probably fix this by adding an intersection type again on the right-hand side. I think there was something else I was missing from TS, but I'll have to noodle on it a bit more.

Re:Show HN: A SQL database implemented purely in TypeScript ... by dllthomas on Monday September 28, 2020 @04:46PM (#24621039)
Not dumb, you answered the question I asked :D

But yeah, I agree that duplicating the intersection produces another interesting question.

In any case, "convenient approximation of row types" probably still applies to TS more than Haskell. And possibly also "more convenient row types", but that remains TBD.

Re:Show HN: A SQL database implemented purely in TypeScript ... by tegeek on Monday September 28, 2020 @12:16PM (#24618257)
Most typed languages work like this way. For example, Java compiles down to the bytecode. The JVM bytecode doesn't have preserve generics information. But Java code has generics so when it compiles down to the bytecode, it removes that information. This is almost true for all high level languages. Kotline's type system is very different than Java but it also compile down to the bytecode so Java and Kotline shares their libraries without sharing the type system. Type Script is no different. Only problem here is that Type Script doesn't bring its own "runtime" or libraries.
Re:Show HN: A SQL database implemented purely in TypeScript ... by vlang1dot0 on Monday September 28, 2020 @12:58PM (#24618746)
> These kinds of “wacky types” are things you can statically analyze but not really “compile” per se. That’s why it’s a good fit for being transpiled down to a language with no types at all.

> TL;DR, TypeScript is not really a “typed programming language”, it’s a static analysis tool. It’s not really designed to “run”.

I think you have an interesting point but you've come to the wrong conclusion. Assembly language doesn't really have any notion of types (there is some notion of sizes as some instructions operate on, for example, single words and some instructions like SIMD ones operate on multiple words at a time) yet we still consider C, C++, Rust, Pascal, etc to be "typed programming languages". It's really only languages that compile to some kind of VM which have a notion of types at the runtime level and even then, many VMs erase some of those details like the JVM and generics.

Re:Show HN: A SQL database implemented purely in TypeScript ... by dvt on Monday September 28, 2020 @01:09PM (#24618875)
This isn't strictly true, and @erikpukinskis has a point. Typed languages often do have type info that can make it to runtime. `typeof` -- virtually the only tool JS has for type reflection -- doesn't even compare to the richness of TypeScript. All of that richness is indeed lost in transpilation.

In C++, for example, we have `decltype`, `typeid`, type traits, RTTI, and so on -- all of which are available at runtime. Not to mention that in certain special cases, we also have the de facto storage of type information that makes it into binaries (e.g. discriminated union types).

Re:Show HN: A SQL database implemented purely in TypeScript ... by still_grokking on Monday September 28, 2020 @07:30PM (#24622410)
You don't need any type information at runtime if your type-system is strong enough. You need only types at runtime to do reflective checks, and you need those only if your type-system can't give the needed guaranties statically at compile time.
Re:Show HN: A SQL database implemented purely in TypeScript ... by jey on Monday September 28, 2020 @09:44AM (#24616510)
If you're impressed with TypeScript, you should definitely check out ReScript (formerly BuckleScript). Its type system is clean and sound, and yet compiles to native JS without overhead. https://rescript-lang.org/docs/manual/latest/introduction
Re:Show HN: A SQL database implemented purely in TypeScript ... by cjdell on Monday September 28, 2020 @10:26AM (#24616923)
Ah yes. I looked at this a while ago. I liked it but then ran into a brick wall with a lack of support for an equivalent to async/await syntax. It has wrappers for JS promises but they are clunky and awkward, unless someone wants to enlighten me?
Re:Show HN: A SQL database implemented purely in TypeScript ... by jey on Monday September 28, 2020 @10:47AM (#24617181)
Try this for some syntactic sugar: https://github.com/digitake/bs-promise-monad
Re:Show HN: A SQL database implemented purely in TypeScript ... by chrischen on Monday September 28, 2020 @03:27PM (#24620347)
Just want to add that the genType project can even compile ReScript to Typescript!
Re:Show HN: A SQL database implemented purely in TypeScript ... by nsonha on Tuesday September 29, 2020 @03:12AM (#24624932)
don't get your hope too high it's just the intersection between the two, advanced typescript types are lost.
Re:Show HN: A SQL database implemented purely in TypeScript ... by tegeek on Monday September 28, 2020 @10:03AM (#24616680)
TS compiled to JS without any "overhead". This is not what the OP said.
Re:Show HN: A SQL database implemented purely in TypeScript ... by phpnode on Monday September 28, 2020 @09:46AM (#24616528)
I don't think even ReScript can do this (yet?)
Re:Show HN: A SQL database implemented purely in TypeScript ... by nsonha on Tuesday September 29, 2020 @01:27AM (#24624414)
given it's just ocaml I don't think it can do this ever, not that it's necessarily a bad thing.
Re:Show HN: A SQL database implemented purely in TypeScript ... by ricksharp on Monday September 28, 2020 @04:24PM (#24620867)
Wow, when I read this earlier, I was dismissive (because I am really happy with typescript). But reading through the comparison - it seems to have many of the great parts I like in Typescript.

Then, I noticed it has the pipe operator! I'm going to be giving this a try.

Re:Show HN: A SQL database implemented purely in TypeScript ... by tegeek on Monday September 28, 2020 @09:51AM (#24616567)
I had been thinking that WebAssembly or CLR could be the target for TypeScript and that would allow to bring her own base class libraries etc.

But if you look closely again, the beaut & wide adoption of TS is because it has no "runtime" or no extra libraries or APIs to learn. Its the JS.

This also makes it easy to iterate quickly on Type System and bring new features. Having its own runtime means, that wouldn't be easy.

But I completely agree that TS right now has perhaps one of the best and most intuitive Type System than any other language.

Re:Show HN: A SQL database implemented purely in TypeScript ... by dynamite-ready on Monday September 28, 2020 @01:36PM (#24619203)
Interesting referencing a programming language as a feminine article like that... Not saying it's wrong, just unusual to me.
Re:Show HN: A SQL database implemented purely in TypeScript ... by dynamite-ready on Monday September 28, 2020 @06:08PM (#24621747)
People downvoting an observation on a curious use of language? If it's because of some perceived political motive, then that's really sad.

I really did just wonder why anyone would anthropomorphize a programming language, by ascribing a gendered pronoun to it.

Re:Show HN: A SQL database implemented purely in TypeScript ... by bmarkovic on Tuesday September 29, 2020 @11:14AM (#24628792)
They're probably a native speaker of gramatically gendered language (like German, most Slavic languages etc).
Re:Show HN: A SQL database implemented purely in TypeScript ... by bruh2 on Tuesday September 29, 2020 @07:42AM (#24626467)
My native languages has gendered objects, and "language" is feminine. Might be the same case
Re:Show HN: A SQL database implemented purely in TypeScript ... by aszen on Monday September 28, 2020 @11:56AM (#24618016)
I'm a bit torn on whether an advanced type system like TS produces better api designs for libraries or whether it promotes bad designs that need advanced typings to be safe and sound.

I feel typescript is in many ways closely tied to JS because many of the advanced typing features don't make sense outside of existing javascript patterns.

We all talk about simplicity so what makes type systems different that we get excited about their advanced and complicated features.

Re:Show HN: A SQL database implemented purely in TypeScript ... by mattgreenrocks on Monday September 28, 2020 @01:10PM (#24618889)
> I'm a bit torn on whether an advanced type system like TS produces better api designs for libraries or whether it promotes bad designs that need advanced typings to be safe and sound.

Well, I'd argue the type system surfaces the pain faster than the untyped variant. In the untyped version, you have to read the code to see that, "it's either the number 4, or a function which may produce the number 4."

Once you see you need a weird type signature to represent 4 OR a function which produces a number, then your spidey-sense kicks in and says, "this is getting gross, maybe I should either always return just the number, or always return a function which produces the number." This avoids a branch in the code that consumes the result where you check the type and do something based on that. At a meta-level, this is the type system telling you something about your program that you might otherwise miss if you were so focused on getting something working.

Re:Show HN: A SQL database implemented purely in TypeScript ... by aszen on Monday September 28, 2020 @01:32PM (#24619154)
I agree on type systems being helpful, my point was about immensely flexible type systems like TS which don't force any usual constraints of designing with sound types. I do appreciate a good sound type system that constraints the behavior of my code in well expected ways.
Re:Show HN: A SQL database implemented purely in TypeScript ... by throwanem on Monday September 28, 2020 @01:01PM (#24618791)
My experience has been that complex APIs will be complex whether they're statically typed or not. It's always possible to add accidental complexity to a design, but an essentially complex problem domain necessitates an essentially complex solution. If that solution includes a static type system, then the type system needs to be at least as complex as the domain.

I'd be interested to hear more about the TS features that you find to be JS-bound. My experience with TypeScript gets deeper every day, but I haven't yet run into anything that was obviously only necessary because TypeScript is a layer over Javascript. I'd appreciate the benefit of some additional perspective there.

Re:Show HN: A SQL database implemented purely in TypeScript ... by aszen on Monday September 28, 2020 @02:01PM (#24619483)
Sure, let me start with a disclaimer, I have used TS briefly and found it great but I personally prefer Elm's type system.

My understanding is TS was designed specifically to statically type existing JS patterns in popular libraries and frameworks and while that's great, newer libraries rarely need such flexibility if they aim for type safe designs from the start.

When I use typescript I often get the feeling there are multiple ways of typing the same function, which is reasonable given that one of TS goals was to embrace the JS ecosystem and that meant covering multiple overlapping use cases.

Consider Enums and Union types in TS, at a higer level both express the same thing (this value can be one of these sets of values) but in practice there is lots of discussion on when to use which. I have not seen sum types (as they are usually called) being implemented in two different ways in other languages.

I think one of the ways to think about TS is that it was designed to help programmers better understand their JavaScript code by adding types while reinforcing their designs in a safe manner so if TS didn't have to interface with existing JS code many of the cool tricks it's picked up would not be useful I think.

Finally on the point of expressivity, I'm interested to know how TS helps in modelling application logic to make invalid state impossible.

Re:Show HN: A SQL database implemented purely in TypeScript ... by PudgePacket on Monday September 28, 2020 @08:44AM (#24616018)
What do you mean by "no JS overhead"?
Re:Show HN: A SQL database implemented purely in TypeScript ... by cjdell on Monday September 28, 2020 @08:59AM (#24616141)
I'm glad you asked. :-) For a start, number types. I want i32, i64, u32 etc. JavaScript (and therefore TypeScript) only has "number". Yes we now finally have BigInt but it's not ideal for JIT optimisation.

Object prototypes is a weird part of JS that we really could do without. If you really want that, use class syntax.

The Date object. Need I say more...

Little things like Object.keys() should return a (keyof T)[] rather than a string[] but can't due to JS edge cases.

Want first class tuples/immutable arrays.

Many other new syntaxes that can't be implemented due to the need for JS compatibility.

I'm sure there are others that I can't think of right now...

Re:Show HN: A SQL database implemented purely in TypeScript ... by ricksharp on Monday September 28, 2020 @09:42AM (#24616499)
Object.keys() is always frustrating.

Also, the array access not returning an optional type (I wish there was a strict option for that) often gets me.

We need a middle ground: “TypeStrict“ that can clean up some of the edge cases.

If you need strict integers, you can create your own fake type:

type Int32 = number & {__type:'Int32'};

I do this with strings sometime when I want a string subtype that can’t be accidentally assigned by a normal string.

Re:Show HN: A SQL database implemented purely in TypeScript ... by svieira on Monday September 28, 2020 @10:59AM (#24617334)
In TS 4.1+ there _is_ an option for strict array access called `noUncheckedIndexedAccess` [1]

[1]: https://devblogs.microsoft.com/typescript/announcing-typescr...

Re:Show HN: A SQL database implemented purely in TypeScript ... by ricksharp on Monday September 28, 2020 @02:09PM (#24619568)
Awesome!

I was just searching for that and all I could find were old github issues saying it was working as intended.

Thank you!

Re:Show HN: A SQL database implemented purely in TypeScript ... by inbx0 on Monday September 28, 2020 @10:15AM (#24616814)
Good news, safe array access is coming in TS 4.1

https://devblogs.microsoft.com/typescript/announcing-typescr...

Re:Show HN: A SQL database implemented purely in TypeScript ... by williamdclt on Monday September 28, 2020 @10:06AM (#24616712)
For anybody interested in this technique: the googleable term is "branding". It's bringing some nominal typing into TS's structural typing
Re:Show HN: A SQL database implemented purely in TypeScript ... by phpnode on Monday September 28, 2020 @10:08AM (#24616745)
I wrote an article about this a while ago: https://codemix.com/opaque-types-in-javascript
Re:Show HN: A SQL database implemented purely in TypeScript ... by ricksharp on Monday September 28, 2020 @10:53AM (#24617257)
Cool, I never knew what that was called. Thanks!
Re:Show HN: A SQL database implemented purely in TypeScript ... by inbx0 on Monday September 28, 2020 @09:37AM (#24616459)
For new code, I think most JS devs have already switched to using the class syntax. Also FP in JS is alive and well if you want to avoid dealing with prototype chains altogether.

I think we can all agree the Date API sucks, but life can still be good if you just give in and include a date library. Also, there's some light at the end of the tunnel with Temporal proposal coming along [1].

Object.keys returning string[] is purely a TypeScript design decision, coming from how TypeScript chooses to model object subtyping [2].

  type Foo = {
    a: string
  }
  
  const keysOfFoo = (obj: Foo) => Object.keys(obj)
  
  const foo = {a: '', b: 5}
  keysOfFoo(foo)
The fact that this passes type checks is a conscious TS design decision that comes with both advantages and disadvantages. It wouldn't have to be this way; Exact types [3] could potentially be used to describe that if the type is exactly Foo, it's safe to assume Object.keys(foo) is (keyof Foo)[].

For first class tuples (and records), there's a stage 2 EcmaScript proposal coming along [4]. There's of course also the readonly [] type in TypeScript if you only need the safety of not accidentally pushing to an "immutable" array. First class language support could have nice additional features though, like strict equality.

Anyways, if these are the things you don't like about JS/TS, I don't think AssemblyScript will be the answer for you, as the goals of that language seem to be entirely different from "fixing" old JS cruft. Apart from the i32, i64 stuff I guess.

[1] https://github.com/tc39/proposal-temporal

[2] https://github.com/Microsoft/TypeScript/pull/12253#issuecomm...

[3] https://github.com/microsoft/TypeScript/issues/12936

[4] https://github.com/tc39/proposal-record-tuple

Re:Show HN: A SQL database implemented purely in TypeScript ... by maxgraey on Tuesday September 29, 2020 @06:13AM (#24625865)
> I don't think AssemblyScript will be the answer for you, as the goals of that language seem to be entirely different from "fixing" old JS cruft. Apart from the i32, i64 stuff I guess.

Basically AssemblyScript try to fix JS runtime as well. See this: https://twitter.com/AssemblyScript/status/114604718874680934... and https://twitter.com/AssemblyScript/status/114466636945316659...

Re:Show HN: A SQL database implemented purely in TypeScript ... by IggleSniggle on Monday September 28, 2020 @12:33PM (#24618431)
That pull request from 4 years ago with regards to Object.keys is worth reading if you already or are considering writing a “type wrapper” for Object.keys, especially now that better tuple types have landed and string template types are coming.

Runtime types can still get you there. I’ve been happy with myzod for this purpose, which feels like writing TypeScript while getting you everything you need to validate at runtime in a very performant package.

Re:Show HN: A SQL database implemented purely in TypeScript ... by h0h0h0h0111 on Monday September 28, 2020 @09:12AM (#24616258)
Another potential benefit - runtime metaprogramming. All types are erased when converted to JS, but I can imagine a lot of utility for runtime types in Typescript.
Re:Show HN: A SQL database implemented purely in TypeScript ... by williamdclt on Monday September 28, 2020 @09:31AM (#24616415)
Obligated mention of runtypes (https://github.com/pelotom/runtypes) that bridges the gap between type-land and runtime-land. It's not seamless at all but if you have a serious need for this sort of thing, it's great
Re:Show HN: A SQL database implemented purely in TypeScript ... by h0h0h0h0111 on Friday October 02, 2020 @09:31AM (#24662376)
Not quite the same, but reminds me a bit of yup (https://github.com/jquense/yup)!
Re:Show HN: A SQL database implemented purely in TypeScript ... by faichai on Monday September 28, 2020 @10:10AM (#24616759)
I think there is some experimental decorator syntax which will allow for extracting type info. Does runtypes interop with that? Or is planning to?
Re:Show HN: A SQL database implemented purely in TypeScript ... by phpnode on Monday September 28, 2020 @10:12AM (#24616776)
there's the reflect-metadata package but the type information it exposes is very limited, "object" instead of {a: "foo"} and so on.
Re:Show HN: A SQL database implemented purely in TypeScript ... by p4bl0 on Monday September 28, 2020 @12:44PM (#24618567)
Why not use a language that has the benefits of TS + everything you say and is designed to be independent from JS.

I'm thinking of OCaml for example. Why not use OCaml rather than TS?

Edit: Actually the idea is so obvious that you have ReasonML that can compile both to JS and assembly and is basically OCaml with a JS-like syntax: https://reasonml.github.io/docs/en/what-and-why

Isn't that exactly what you're thinking of?

Re:Show HN: A SQL database implemented purely in TypeScript ... by int_19h on Monday September 28, 2020 @08:23PM (#24622791)
OCaml doesn't have all the benefits of TS, when it comes to the power and the flexibility of its type system.
Re:Show HN: A SQL database implemented purely in TypeScript ... by BreakfastB0b on Monday September 28, 2020 @09:50AM (#24616561)
Little things like Object.keys() should return a (keyof T)[] rather than a string[] but can't due to JS edge cases.

This was something that confused me when I first started learning Typescript but is actually not an edge case but a fundamental feature of Typescript's structural typing.

A type T is only guaranteed to be at least (a subtype of) T. Which in the case of objects means it has at least those fields, but potentially more. For instance

  interface FooBar {
    foo: string
    bar: string
  }

  interface Baz {
    baz: number
  }

  const fooBarBaz: FooBar & Baz = {
    foo: 'foo',
    bar: 'bar',
    baz: 3
  }

  
  const printFoobar = (fooBar: FooBar) => {
    for (const key of Object.keys(foobar)) { 
      if (key !== 'foo' && key !== 'bar') {
        // If Object.keys was (keyof T)[] Typescript would claim this branch is unreachable
      } 
    }
  }

  printFooBar(fooBarBaz)
It's really worth thinking about Typescript types not as nominal objects like in Java or Haskell, but contracts about minimum functionality. Embracing this in terms of function arguments and return types makes testing and composing typescript code much easier. For example, if you were writing a AWS Lambda function in Typescript that only uses the body of the incoming event.

  export const handlerOne = (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
    console.log(event.body)

    return {
      statusCode: 200,
      body: event.body,
    }
  }

  interface MinimalEvent extends Pick<APIGatewayProxyEvent, 'body'> {}

  export const handlerTwo = (event: MinimalEvent): Promise<APIGatewayProxyResult> => {
    console.log(event.body)

    return {
      statusCode: 200,
      body: event.body,
    }
  }
handlerTwo only requires a handlerTwo({ body: '....' }) call in a test, instead of having to fill in all the extra properties of the APIGatewayProxyEvent that aren't even required. You might be tempted to use a type coercion in the test instead e.g. handlerOne({ body: '....' } as APIGatewayProxyEvent) but the problem with the coercion is that is in not checked at all by the compiler, it's essentially like temporarily using an any type. So if handlerOne is updated in the future to depend on more of the structure of APIGatewayProxyEvent Typescript will be none the wiser that the test requires updating (although granted hopefully the test would fail).

Anyway, that's a long winded explanation of why Object.keys shouldn't return (keyof T)[]. If you want to program generically over the Type of something I'd suggest making use of a runtime type library like io-ts instead which gives you a data structure representing the type to iterate over, etc.

Re:Show HN: A SQL database implemented purely in TypeScript ... by bengillies on Monday September 28, 2020 @09:40AM (#24616481)
> Want first class tuples/immutable arrays.

On this subject specifically, see the records & tuples proposal (currently at stage 2): https://github.com/tc39/proposal-record-tuple

Re:Show HN: A SQL database implemented purely in TypeScript ... by ballenf on Monday September 28, 2020 @12:07PM (#24618146)
When you say doing without object prototypes -- are you advocating moving away from the entire prototypal inheritance model? Or just not exposing it?

Your requirements list makes me wonder why you want to stick with anything related to JS at all? With all the transpiling options, web assembly, etc.

Re:Show HN: A SQL database implemented purely in TypeScript ... by mind-blight on Monday September 28, 2020 @10:37AM (#24617060)
What are the edge cases for Object.keys()? I've always wondered why that returned string[]
Re:Show HN: A SQL database implemented purely in TypeScript ... by maple3142 on Monday September 28, 2020 @12:12PM (#24618221)
Example:

  const x = { a: 1, b: 2, c: 3 }
  const y: { a: number, b: number } = x
  console.log(Object.keys(y))
Re:Show HN: A SQL database implemented purely in TypeScript ... by z3t4 on Monday September 28, 2020 @11:21AM (#24617579)
Why not make TS compile to LLVM?
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...

Re:Show HN: A SQL database implemented purely in TypeScript ... by akmittal on Monday September 28, 2020 @08:54AM (#24616093)
Probably running raw TS not TS -> JS -> Compiler flow. Running TS directly will have lots of performance benefits. Compiler will know the types and can optimize better.
Re:Show HN: A SQL database implemented purely in TypeScript ... by Wilem82 on Monday September 28, 2020 @10:14AM (#24616799)
Is this satire? What do you think is advanced about TS' type system and why?

Duck-typing that creates a minefield instead of providing correctness?

The unknown type that does the same?

TS is a step forward from JS, but JS's bar is so infamously low that making something better isn't a big achievement, especially compared to other languages with normal type systems.

Re:Show HN: A SQL database implemented purely in TypeScript ... by kingdomcome50 on Monday September 28, 2020 @10:24AM (#24616902)
I'm honestly curious. What other languages have a type system the would allow for this project? That is, (a subset of) SQL as a type. And assuming these languages exist, are they as ergonomic to the developer as TS while instrumenting the above?
Re:Show HN: A SQL database implemented purely in TypeScript ... by Wilem82 on Monday September 28, 2020 @01:08PM (#24618873)
Are you really calling this ergonomic? https://github.com/codemix/ts-sql/blob/master/src/Evaluator....

Same thing but actually readable and maintanable would've been better implemented as a compile-time (build-time) script, basically source code generation.

Re:Show HN: A SQL database implemented purely in TypeScript ... by zamadatix on Monday September 28, 2020 @01:37PM (#24619208)
Nobody said it's ergonomic to implement SQL in a type system. It was simply an answer to your question "What do you think is advanced about TS' type system and why?". It's advanced because you're able to implement SQL in types alone when with most languages you couldn't. The question on ergonomics is for the other languages where you can do this is it more or less ergonomic than this. Again that doesn't mean this IS ergonomic it's about finding a language with a powerful type system that could do it MORE ergonomically.
Re:Show HN: A SQL database implemented purely in TypeScript ... by Wilem82 on Monday September 28, 2020 @03:37PM (#24620429)
This thread made me read about conditional types in TS and in general more about its type system. I was ignorant. I agree now that it's advanced. Previously I only heard bad things about it without bothering to study it myself. Turns out, it's not all bad, there is some good as well. Still wouldn't want to work with it, ever; but that's beside the point.
Re:Show HN: A SQL database implemented purely in TypeScript ... by kingdomcome50 on Monday September 28, 2020 @02:03PM (#24619500)
Well said. It's amusing imagining the soup that would result from attempting to do this sort of thing in various other languages!
Re:Show HN: A SQL database implemented purely in TypeScript ... by kingdomcome50 on Monday September 28, 2020 @01:40PM (#24619243)
Yes. What you don't see it that the TS language server provides things like autocomplete, definitions on hover, squiggly lines, sane error messages, etc. to the developer as they write the parsing code. What you don't see is that because that file compiles, it is guaranteed to parse (certain) "SQL" strings into types. This is because the code doesn't have to actually run to work, rather, TS's powerful type system does the parsing.

Source code generation wouldn't really provide the same end-user ergonomics and, frankly, would barely resemble the same project. How would that work? You generate source code from a SQL string and a data source? At that point you might as well just... write the actual code. The point of this package is that type information is dynamically assigned according to arbitrary "SQL" queries at compile-time[0].

Have you gone through short exercise I outlined below? Surely you can't think it would be better to manually generate type information than have it automagically available and enforced instantly as you work?

The closest thing I've seen to this is "type providers" in F#. Where, given a database connection, a "Provider" can offer compile-time contracts between your code and the schema of the database. What it does not do is provide contracts against arbitrary projections of the database (SQL queries). Of course one can write code that queries and transforms the data in a type-safe way, but these "transformations" must happen in F# for the compiler to infer any new types.

This project takes the above to a different level and infers the type of the result of an arbitrary (subset of) SQL operation before it is ever executed. It's fairly impressive.

[0] This has been pointed out more than once already, but this project does offer compile-time guarantees

Re:Show HN: A SQL database implemented purely in TypeScript ... by rimliu on Monday September 28, 2020 @11:16AM (#24617529)
Not sure if it makes it possible, but this is fun anyway: https://aphyr.com/posts/342-typing-the-technical-interview
Re:Show HN: A SQL database implemented purely in TypeScript ... by Wilem82 on Monday September 28, 2020 @10:35AM (#24617022)
What compile-time guarantees does that SQL library give? I don't see any docs or explanations at that Github repo and my TS knowledge is limited to be able to infer everything from a small code example.
Re:Show HN: A SQL database implemented purely in TypeScript ... by kingdomcome50 on Monday September 28, 2020 @11:08AM (#24617447)
Maybe clicking on the playground link would help your understanding of the project. You can literally hover over the types defined like:

      type EX = Query<"some query string", db>; 
and see the that the type as defined by the TS language server (i.e. in your editor) is the result of "running" the query without actually running the code. It's a little insane and somewhat akin to "type providers" in languages like F#.

For example, add this below `EX1` (line 66) in the TS Playground:

    let names = (persons: EX1) => persons.map(person => person.name);
Now change the column alias in `EX1` to something other than "name" and see what happens. You see that? There is now a compile-time error.
Re:Show HN: A SQL database implemented purely in TypeScript ... by phpnode on Monday September 28, 2020 @10:55AM (#24617286)
all of the functionality happens at compile time
Re:Show HN: A SQL database implemented purely in TypeScript ... by jariel on Monday September 28, 2020 @01:25PM (#24619068)
TS is really getting Ninja with typing.

The ultra expressiveness of TS may not be a virtue. It may simply encourage us to be creatively academic with our designs, and do novel, non-obvious and hard to understand and maintain things in the code.

With some of the newer TS releases, I can hardly fathom through the release notes why such a complex degree of metaprogramming has utility for anyone, and what kind of corner cases people must be diving into. They are actually quite hard to follow, and I find myself trying to parse through the need for such a thing, literally unable to conceive of an example use case. Perhaps without a strict philosophy such as they have in Go, the designers just go on ahead and implement their favorite intellectual exercise, or something that helps them with a task in the TS compiler itself, which may not generalize very well.