7
kobenz
2d

oh boy

import type { ParseJson } from "json-parser-in-typescript-very-bad-idea-please-dont-use"

type Person = ParseJson<'{ "name": "Jamie Kyle", "twitter": "https://twitter.com/buildsghost" }'>
// {
// "name": "Jamie Kyle",
// } & {
// "twitter": "https://twitter.com/buildsghost"
// }

Comments
  • 5
  • 4
    json parsing via const generics.... yeaaaaa....
  • 5
    What the hell?!
  • 5
    One of the reasons I love C# (in .net core, not .net framework/standard) is how nicely it plays with JSON.

    class Person {

    string? Name { get; set; }

    }

    ...

    try {

    var person = JSONSerializer.Deserialize<Person>(string);

    }

    catch (JSONParseError jpe) {

    Console.WriteLine("That's not a fucking person!");

    }
  • 5
    Why the fuck does this exist
  • 4
    Seriously someone actually sat down and thought this would be useful
  • 4
    @AlgoRythm Obligatory Rust plug :P

    #[derive(Deserialize)]

    struct Foo {

    #[serde(rename = "qux")]

    pub is_qux: bool,

    #[serde(default)]

    pub list: Vec<Bar>,

    }

    let x = serde_json::from_str::<Foo>("...");

    One of the most beautiful things i've ever used. It's so flexible and compatible with pluggable formats
  • 2
  • 1
    Insanity aside, that dude did write the smallest compiler I have EVER had the displeasure of reading
  • 3
    Swift

    ```
    struct Person: Codable {
    let name: String?
    }

    let person = try? JSONDecoder().decode(Person.self, data: jsonData)
    ```

    This works with other formats as well, by using different decoders like XML or INI, etc.

    And it‘s completely customizable. You can change naming like CamelCase or snake_case (useful for encoding), pretty printed with line breaks or compact single line, encoding/decoding rules for specific types like dates, and you get rich and detailed error messages telling you exactly what is wrong.
  • 1
    That must be just someone who's doing an exercise on TS types - because there's quite a lot that can be done on the type level. I see nothing wrong with that.
  • 5
    @Lensflare @TeachMeCode I bet neither of you know that you can play doom using the typescript type system, it's incredibly powerful. I don't think there is even a comparative system out there

    @AlgoRythm @12bitfloat @Lensflare Not what this does
  • 1
    @BordedDev I mean it parses json via the type system from a generic parameter, does it not? Either way this is cursed lol
  • 1
    I remain broadly convinced that TypeScript is a little gimp.
  • 2
    @BordedDev that’s pretty cool actually. I had no idea you can do that and I’ve had two jobs that used typescript lol. That plus I like playing doom games
  • 3
    @BordedDev I have no doubt that it’s powerful.
    And being able to run doom isn’t special. Anything that‘s Turing complete can run doom.
  • 3
    @Lensflare And yet your mom still struggles. It's pretty special when it comes to type systems.

    @12bitfloat It makes the type the parsed json, which is not what your code does
  • 2
    All I know about doom is shredding monsters with chainguns and bfgs is hella fun
Add Comment