Generating TypeScript types from XML: why hand-writing them doesn't scale
By the Converterzilla Team
We build privacy-first PDF and image tools that run entirely in your browser. Our team has shipped JavaScript file-processing apps used by thousands every day, and we write here about the libraries, trade-offs and patterns we use.
Consuming an XML API or feed in a TypeScript codebase usually starts the same way: someone looks at a sample response and hand-writes an interface for it. That interface is correct for exactly the sample it was written against, and starts drifting the moment real traffic includes a field the sample didn't have, or an optional element the sample happened to include.
What type inference from a real document gets you
Generating the interface directly from an actual XML document — rather than from memory or a spec skim — grounds the types in something real: object nesting becomes nested interfaces, repeated elements become array types, and primitive values get typed as string, number, or boolean based on what's actually in the sample, not what someone assumed the format used.
The same caveat as JSON Schema inference
This has the identical limitation as generating a JSON Schema from one document: a type inferred from a single sample reflects that sample, not every valid document the format allows. A field absent from your sample won't appear in the generated interface at all; a field that's a plain string in your sample but occasionally numeric in other real documents will be typed too narrowly. Generated types are a strong starting point grounded in real data, not a substitute for validating against a few more examples — especially ones you know contain edge cases.
Where generated types are most useful
- Prototyping against a real API before a formal contract exists — get moving with types that reflect actual responses rather than guesswork.
- One-off scripts and data migrations where round-tripping through a hand-maintained interface definition isn't worth the overhead.
- Sanity-checking a hand-written interface — generate one from a real sample and diff it against what's already in the codebase to spot drift.
Our XML to TypeScript generator infers interfaces — including nested objects and array types — from your XML document locally in your browser. Treat the output the way you'd treat any type inferred from one example: a strong first draft worth checking against a couple more real samples before it goes into a shared codebase.