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 articleIn 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.
Related
Zig-style generics are not well-suited for most languages
Zig-style generics, inspired by C++, are critiqued for limited universality. Zig's simplicity contrasts with Rust and Go's constraints. Metaprogramming praised for accessibility, but error messages and compiler support pose challenges. Limited type inference compared to Swift and Rust.
Improving Your Zig Language Server Experience
Enhance Zig Language Server (ZLS) by configuring it to run build scripts on save for immediate error display. Zig project progresses include faster builds, incremental compilation, and code intelligence. Support via Zig Software Foundation donations.
[0] https://github.com/GoogleFeud/ts-runtime-checks
[1] https://github.com/moltar/typescript-runtime-type-benchmarks
>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/
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 mapWell...
>>> 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)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.
Related
Zig-style generics are not well-suited for most languages
Zig-style generics, inspired by C++, are critiqued for limited universality. Zig's simplicity contrasts with Rust and Go's constraints. Metaprogramming praised for accessibility, but error messages and compiler support pose challenges. Limited type inference compared to Swift and Rust.
Improving Your Zig Language Server Experience
Enhance Zig Language Server (ZLS) by configuring it to run build scripts on save for immediate error display. Zig project progresses include faster builds, incremental compilation, and code intelligence. Support via Zig Software Foundation donations.