Format Code Now
/
ConvertYAML to TypeScript Next: XML XSLT Transformation ›

YAML to TypeScript Interface Generator

CV-94

Paste a YAML document and press Convert to get interfaces with optional and union types. The YAML is read into JSON first, so the types come from its values, and it runs in your browser.

Runs in your browser. Nothing is uploaded.

YAML

TypeScript

How to use it

  1. Paste one YAML document, or open a .yaml or .yml file. Use a sample that shows every section you expect.
  2. Choose interface or type, optional keys, the export keyword and the indent.
  3. Press Convert, then copy the result or download it as a .ts file.
  4. Use the interface as a type for the parsed data, for example JSON.parse(text) as Root; nothing is checked at runtime. The names follow the JSON form of your YAML, so add the tags your YAML library needs if you load YAML directly.

How the interfaces are built

Each object becomes an interface, or a type alias if you choose it, and nested objects get their own named interfaces. Mixed types become a union, and an empty array becomes unknown[]. Nested interface names come from the key, and arrays use the key without its trailing s; the export keyword and indent can be changed. A key missing from some objects becomes optional (?), and a null becomes | null.

How the YAML becomes interfaces

Comments are dropped, only one document is read, and keys that are not text become text. A value written as 1.0 is read as the number 1, and an integer beyond 9007199254740991 is rounded by the YAML reader. .nan and .inf become null. The YAML is parsed with the core schema, converted to JSON, and the code is built from that JSON. Keys that are not valid TypeScript names, such as first-name, are renamed, and the original key is kept in a quoted property name.

What a sample cannot tell it

The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay string, and no runtime check is written, so validate data that comes from outside. A YAML file with optional sections should be pasted with every section present, or the missing sections will not appear in the output.

Questions people ask

How do I convert YAML to a TypeScript interface?

Paste the YAML into the input box, choose your options and press Convert to get the interfaces. Use the interface as a type for the parsed data, for example JSON.parse(text) as Root; nothing is checked at runtime. The interfaces follow the JSON form of the YAML. The conversion happens in your browser.

Does the TypeScript output check data at runtime?

No. Interfaces exist only at compile time, so JSON.parse(text) as Root is a promise to the compiler, not a check. Validate data that comes from outside with a schema validator; the JSON Schema generators on this site can start one for you. This applies to the JSON form of your YAML.

Why is a big integer typed number with a comment in TypeScript?

TypeScript has one number type, and JSON.parse rounds integers beyond 9007199254740991. The line gets a comment so you know the value may already be rounded. Keep such values as strings in the source data. This applies to the JSON form of your YAML.

Interface or type alias for the generated TypeScript?

The output defaults to interface. Switch to type if your code style or lint rule prefers aliases; the fields are the same. Use interface when other code needs to extend it. This applies to the JSON form of your YAML.

Why did a YAML value like 1.0 come out as an integer type?

The YAML is converted to JSON before types are chosen, and 1.0 is the same number as 1 there. A field that always holds whole-looking decimals in your sample can come out as an integer type. Add a non-whole decimal to the sample, or change the type by hand. The TypeScript interfaces are written from that converted data.

Can I paste a YAML file with several documents?

No. Only a single document is read; a file with --- separators between documents gives an error. Paste one document at a time, or merge them into a list under one top-level key yourself. The TypeScript interfaces are written from that converted data.

About this tool

The YAML is read with the core schema (true/false, null, integers and floats), converted to JSON, and the code is generated from that. Dates stay text, comments are dropped, anchors, aliases and << merge keys are resolved, keys that are not text become text, and only one document is read. A value such as 1.0 is read as the number 1, so a field that holds whole-looking decimals can come out as an integer type; an integer beyond 9007199254740991 is rounded by the YAML reader before any code is written; .nan and .inf become null. Every object becomes an interface (or a type alias), and nested objects get their own interfaces named after the key. A key that is missing from some objects becomes optional (?), a null becomes | null, mixed types become a union, and every number is typed number, with a comment on integers beyond 9007199254740991 because JSON.parse rounds them. An empty array becomes unknown[]. It does not add runtime checks. Everything runs in your browser; nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.