YAML to TypeScript Converter

Generate TypeScript interfaces or type aliases from a YAML file, with control over readonly, optional and export modifiers.

YAML
TypeScript
Runs locally in your browser

About the YAML to TypeScript Converter

Config files that a Node service reads at boot are usually typed as any, which means a renamed key becomes a runtime error instead of a compile error. Pointing this generator at the YAML file produces declarations you can import next to the loader, so the compiler checks every property access against the file you actually ship.

Each distinct mapping shape becomes one declaration, named after the key that introduced it, and repeated shapes are reused rather than duplicated. Sequences become array types, and a sequence of mappings becomes an array of the generated element type with the keys of all elements merged, so an optional field present in only some entries is not lost. YAML integers and floats both become number, since TypeScript has one numeric type. Dates resolved by the loader become Date, and an empty value becomes null.

Four switches shape the output. Choose interface for declarations you may want to extend or merge, or type for aliases you intend to compose with unions and intersections. Add readonly when the configuration must not be mutated after loading, add ? when every key should be treated as possibly absent, and turn off export for a local declaration file. Keys that are not valid identifiers are emitted as quoted properties. Starting from JSON instead? Use JSON to TypeScript.

How to use

  1. Drop in the YAML file you want types for, or upload it from disk.
  2. Choose interface or type and name the root declaration.
  3. Tick readonly or optional if your loading code needs those modifiers.
  4. Copy the declarations or download model.ts.

Common questions

Why is every number typed as number?
TypeScript has a single numeric type, so YAML integers and floats both map to number. Use a branded type if you need to distinguish them.
How are keys with dashes handled?
They are emitted as quoted properties such as "retry-after": number;, which stay valid and accessible with bracket notation.
Should I use interface or type?
Interfaces support declaration merging and extends clauses. Type aliases compose better with unions, intersections and mapped types. Either works for plain config shapes.
What if two sections have the same key names?
They collapse into one declaration, since the shapes are identical. Differing shapes get numbered names such as Owner2.