About the XML to TypeScript Converter
Typing a third party XML response by hand is tedious and easy to get subtly wrong, especially when a list has two entries in the sample and you forget that the parser will hand you an array. This generator infers the structure for you. Repeated sibling elements become arrays, nested elements become their own named interface, and leaf values are typed by inspecting what they contain: digits become number, true and false become boolean, empty elements become null and everything else stays a string.
Names are derived from the tags. A repeated book element inside catalogue gives you a Book interface referenced as Book[], and a plural container name is singularised so you do not end up with a type called Books describing one record. Two elements with the same tag but different children get numbered names rather than being silently merged, so nothing is lost.
Attributes are folded in as ordinary properties. Choose a prefix in the toolbar if an attribute and a child element share a name in your document and you need to tell them apart. The optional and readonly switches matter because XML is loose by nature: a field absent from your sample may still appear in production, so marking everything optional is the honest starting point for an unfamiliar feed. If your source is JSON rather than XML, JSON to TypeScript covers that, and XML to JSON Schema produces a validator instead of types.
How to use
- Paste an XML sample that includes at least two entries of any repeating element.
- Set a Root type name if you want something other than the root tag name.
- Tick Mark every property optional when the sample may not show every field.
- Copy the interfaces or download them as a
.tsfile.
Common questions
- Why is a field typed as number when it holds a product code?
- A value made only of digits is inferred as a number. Set the attribute prefix or edit the generated type if the code should stay a string.
- How are repeated elements handled?
- They become an array of a single interface built from the union of the keys seen across every occurrence in your sample.
- Do I get types for attributes?
- Yes. Attributes become properties alongside child elements, with an optional prefix so they can be told apart.
- Should I choose interface or type alias?
- Interfaces can be extended and merged, which suits models. Type aliases are handier when you plan to build unions from them.