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.
The Approach
Here's a simplified version of the code that attempts to solve this problem:
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
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, 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". 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.
Top comments (0)