XML to TypeScript
Generate TypeScript interfaces or type aliases from an XML sample.
XML to TypeScript – Interfaces for parsed XML
The XML to TypeScript converter writes interfaces (or type aliases) for the object you get after parsing XML in JavaScript. Use them to type the result of fast-xml-parser, xml2js, xml-js or your own DOM walker, and get autocompletion for every element and attribute.
How XML maps to types
XML has no native types, so the converter reads every element and attribute value and infers one: whole numbers become integers, decimals become floating-point numbers, true/false become booleans, and everything else stays a string. Attributes are kept apart from child elements (they appear as @name keys internally), an element that has both attributes and text gets a dedicated text field, and repeated sibling elements such as several item nodes become a list. Namespace prefixes are stripped from names so ns:customer and customer produce the same type.
Matching your parser
The generated interfaces use "@name" for attributes and "#text" for element text, which matches fast-xml-parser configured with { ignoreAttributes: false, attributeNamePrefix: "@", textNodeName: "#text", parseAttributeValue: true }. If your parser uses a different prefix (for example @_ or $), search and replace the prefix in the output. Keys that are not valid identifiers are quoted automatically.
Options
Choose interface or type, add export keywords, mark properties readonly for immutable data, and turn nullable values into optional properties. Repeated elements become arrays such as item: Item[]; a wrapper element becomes its own interface.
Example uses
Typing RSS and Atom feeds in a Next.js site, reading sitemap.xml in a build script, consuming a SOAP API from Node.js, or migrating an old XML configuration to a typed TypeScript config loader.
Tips
Make sure your parser treats repeated elements as arrays even when only one element is present (fast-xml-parser's isArray option), otherwise the runtime value may not match the generated Item[] type.
Related tools
Convert the document itself with XML to JSON or type JSON responses with JSON to TypeScript.
Frequently Asked Questions
Is my XML uploaded anywhere?
No. The XML is parsed and converted by JavaScript running in your browser tab. Nothing is sent to a server, so internal feeds, SOAP payloads and configuration files stay on your machine.
Which XML parser should I use with these types?
Any parser that outputs plain objects. fast-xml-parser with attributeNamePrefix "@" and textNodeName "#text" matches the generated keys exactly; for xml2js or xml-js adjust the attribute prefix.
Are numbers typed as number or string?
The generator infers number and boolean from the text. Enable value parsing in your parser (parseTagValue / parseAttributeValue) so the runtime values match, or change the types to string.
Can I generate type aliases instead of interfaces?
Yes. Set Declaration to type to get type Order = { ... } aliases.