July 17th, 2024

What can TypeScript learn from Zig? What can Zig learn from TypeScript?

A TypeScript developer discusses using Zig in Advent of Code 2023. Zig, a modern low-level language, emphasizes safety with features like option types and compile-time execution. TypeScript could benefit from Zig's error detection approach.

Read original articleLink Icon
What can TypeScript learn from Zig? What can Zig learn from TypeScript?

In a blog post discussing the Advent of Code 2023, a TypeScript developer shares insights on their experience using Zig for the competition. Zig, a low-level programming language introduced in 2016, is compared to C but with modernized features like a reasonable module system and no null pointers. The post highlights Zig's emphasis on safety and modern practices, such as option types and type inference. Noteworthy features of Zig include "Detectable Illegal Behavior" and "comptime," which allow for static error detection and compile-time execution, respectively. The post explores how TypeScript could benefit from Zig's approach to error detection, especially in scenarios like integer overflows and array bounds checking. By incorporating similar concepts, TypeScript could enhance its type safety and potentially reduce runtime errors. The discussion also touches on the potential for TypeScript to introduce a debug build mode for improved type checking at runtime, akin to Zig's approach. Overall, the comparison between Zig and TypeScript sheds light on different strategies for improving language safety and error detection mechanisms.

Link Icon 7 comments
By @sebws - 3 months
On the author’s point of a “debug build” with runtime type checking, there is ts-runtime-checks [0], which looks to do something similar! I don’t have any experience with it however, just seen it looking at typescript-runtime-type-benchmarks [1]. There’s a few other similar things there too

[0] https://github.com/GoogleFeud/ts-runtime-checks

[1] https://github.com/moltar/typescript-runtime-type-benchmarks

By @jesus-was-here - 3 months
Out of curiosity, how long is your experience with TypeScript? I feel like I have to ask.

>TypeScript does not modify this code when it compiles to JavaScript

TypeScript does not modify any code which is valid JavaScript. Your idea of adding some sort of "debug build" to TypeScript would never be performed by `tsc`, but perhaps by bundlers, etc.

You might want to look up libraries like zod, or even better, Effect: https://effect.website/

By @cdcarter - 3 months
I don't come from TS, but this seems like a good intro to zig from that sort of perspective. There's a whole lot of paradigms to relearn when you switch to something like Zig and this kind of content is useful.

I'll nitpick with one complaint though...

    const values = std.AutoHashMap(Point, u32);
    defer values.deinit();
    try values.put(Point{ .x = 0, .y = 0 }, 1);
    //  ~~~~~~^~~~ error: expected 3 argument(s), found 2
> The mistake here isn't on that line, and it doesn't have to do with the number of arguments. Rather, it's that I forgot to call .init() on the hash map

Well...

    >>> class MyType():
    ...    def test(self, a, b): return a + b;
    ... 
    >>> x = MyType
    >>> x.test(1, 2)
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
    TypeError: test() missing 1 required positional argument: 'b'
How else do you want unbound functions to behave when you haven't constructed an instance of the type yet? (And FWIW, zls in vscode correctly annotates values as having type `type` for me)
By @renox - 3 months
I'm surprised that the list of Zig quirks doesn't include aliasing and functions parameters: the compiler can pass your parameter by value or by reference as it wants so your function behaviour can change in case of aliasing..

An implementation defined behaviour worse than C, that's surprising..

Ada has the same issue, I wonder if Ada users can tell us if this pitfall is an issue or not in practice.

By @sakesun - 3 months
Another language that we can build type with its own language is Python. The problem is that there is no checker for that kind of type, however :)