Exploring TypeScript Type Generation for JSON Paths: An AI-Assisted Journey
Published: 2024-06-27 09:26:58


As developers, we often find ourselves pushing the boundaries of what's possible with our tools. Recently, I conducted an intriguing experiment: creating a TypeScript type generator for multiple, arbitrary JSON path inputs. While the experiment isn't complete, the progress made with AI assistance is noteworthy.

## The Challenge

The goal was to create a TypeScript type that represents the structure of an object based on multiple JSON paths. For example, given paths like `$.dynamodb.NewImage.id.S` and `$.dynamodb.NewImage.url.S`, we wanted to generate a type that accurately represents the nested structure.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j90jszr3xltfk7gp8seq.png)

## The Approach

Here's a simplified version of the code that attempts to solve this problem:

```typescript
type GenerateTypeFromJsonPaths<Paths extends JsonPath[]> = Paths extends [infer Path, ...infer Rest]
  ? Path extends JsonPath
    ? MergeTypes<ParseJsonPath<Path>, GenerateTypeFromJsonPaths<Rest>>
    : {}
  : {};

type ParseJsonPath<Path extends string> = // ... implementation

type MergeTypes<T, U> = // ... implementation

const foo: ResultType = {
  dynamodb: {
    NewImage: {
      id: { S: "foo" },
      url: { S: "foo" },
      comment: { S: "foo" }
    }
  }
};
```

This code uses recursive type definitions to parse JSON paths and attempt to merge the resulting types into a single object type.

You can view and experiment with the full code in the [TypeScript Playground](https://www.typescriptlang.org/play/?#code/C4TwDgpgBAUgzgewHYAUCGwAWUC8UAGAJIQN5zABOAlkgOYC++A3AFAuiRToVwTpYAeACpQIAD2AQkAEzixEqDJgB8uKCPGSZcoqRoAzCBSgAlCOUZQA-KfPAoALihIIANyOt24aACEArlQANtIA8gBGAFYQAMbAQt7CohJSslDk1HSqOCxQ6klaqUQkBkZQANIQIPQAdHpIhsZmFvg5udZQJFAA2mVQNOWVALpO-kGhkTFxCU3AqvStuU6dPX1I6sNplDS0UPSeHNAAska0EPGQcMIAjAA06gBMWR2tK-0A1pUI+upXUAA+UA+IC+Dw2vU0KTkQJBQl+ADJAZ9vkJ7gt2scKKdzuZrj1BncUXjlGinODktpEcDkVc0TZYXiSeV8pDKTDUW12oSyoNGS53BRWHs2AcoABxKRGDBnbwAMQoCAAtvBkPxMJdVXIIRTlYosF1Bk8NcyKV0SsZVXdqlazbZyDzcjZVcbUjrVbTbfYtS6FKr9WiHVAMVjvJdRsFwlFYtiBNxeKqY0plMo7uKXBQpdi5YrXUpLjMk-7HFAw+NI1NIAmeHwlAmsAWOU4+UZWkshV5OEa8F1WgAiQjVaQgJBoBUIaRhaoAOQgAHcAJIKtCnapUaTVADKPZuvf7g+Ho-HU9nC6XEGqfgogQ3W53A6HI7HE+n88Xy+iioVUmA15Yg323ltPxAnLaA8FTSVJEzeUlR9XNazVZRPHfJByCgfQEAQJwmiAkC1BIVo9wfccljRZ8T1OEiOVyVcligdcnB7dCEB7XZtyoqAL0CWj6KgRiMJY+g2Ko98FU-JBgG4himIEtF5lyeYhSAA)


## The AI Contribution

Interestingly, most of this code was generated with the help of AI models like GPT-4 and Claude 3.5 Sonnet. The experiment also drew inspiration from an existing GitHub repository, [jsonpath-ts](https://github.com/sinclairnick/jsonpath-ts), which provides type inference for JSONPath queries.

## Current Limitations

While the structure for each path is correctly generated, the code doesn't yet perfectly merge different branches into a single object type. This highlights an important point about AI-assisted coding: it can get you very far, but deep understanding is still crucial for solving complex problems.

## Reflections on AI and Programming

This experiment brings to mind recent discussions about the changing landscape of software development, such as those outlined in the article ["The Death of the Junior Developer"](https://sourcegraph.com/blog/the-death-of-the-junior-developer). AI is indeed amplifying our capabilities, but it's a double-edged sword. It can accelerate both progress and dead ends.

As we navigate this new era of AI-assisted development, it's clear that a solid understanding of fundamental concepts remains invaluable. AI can generate impressive code snippets, but knowing how to piece them together, debug issues, and push beyond the AI's limitations is where human expertise shines.

## Looking Ahead

While the current implementation isn't perfect, it's remarkable that we can generate such complex types programmatically. As AI tools continue to evolve, experiments like this will likely become more feasible, opening up new possibilities in type-safe programming.

For now, this experiment serves as a fascinating example of the potential - and current limitations - of AI-assisted coding in tackling advanced TypeScript challenges.

---

For those interested in the original prompt that led to this experiment, you can find it [here](https://chatgpt.com/share/3afc2c24-f703-4d9e-b1f7-27f2ab444cf5).