News for hackers. Stuff that matters.
Re:Hegel – An advanced static type checker for JavaScript by hermanradtke on Thursday October 13, 2022 @07:52AM (#33189736)
If the typescript compiler included hygienic macros then you could implement something like Rust's serde crate. Once you have that, then you can write normal looking typescript interfaces paired with a macro that implements runtime parsing of an unknown value. For example:

    import macro { Deserialize } from 'ts-serde'; // I made up "import macro" syntax
    import { parse } from 'ts-serde';

    #[Deserialize] // I borrowed this syntax from Rust
    interface Foo {
      bar: string;
      baz: number;
      qux: Record<string, string | null>;
    }

    
 Now I can do something like:

   const response = await fetch('https://www.example.com');
   const foo = parse<Foo>(await response.json());
This is a much nicer typescript world to live in. I can use vanilla interfaces (or even types). I do not have to learn a separate syntax for runtime checks (such as zod, io-ts, runtypes, etc). If I want to switch from one macro implementation, my scope of changes is the annotation on the interface and the "parse" function. That is much, much easier than switching from io-ts to zod.

There are no comments on this comment yet.