YAML to TypeScript
Generate TypeScript interfaces or types from a YAML sample.
YAML to TypeScript – Types for your configuration
The YAML to TypeScript converter generates interfaces or type aliases from a YAML document. Paste a config file, an OpenAPI example, a Docker Compose file or a Front Matter block and get types you can use to give parse(yaml) results real autocompletion.
How YAML maps to TypeScript
The document is parsed with the YAML 1.2 core schema, anchors and aliases are resolved and comments are ignored. Mappings become interfaces, sequences become arrays such as Item[], numbers become number, booleans boolean, and strings string. Lists of mappings are merged into one interface, keys that are missing from some items become optional with ?, and lists that mix different scalar types become unknown[] so you decide the element type yourself.
Options
Choose between interface and type declarations, add or remove the export keyword, mark every property readonly for immutable config objects, and treat null values as optional. Nested objects get their own named types derived from the key name, so the output stays readable.
Loading YAML in TypeScript
With the yaml package you can write const config = parse(fs.readFileSync('config.yaml', 'utf8')) as Config. The cast is a compile-time promise only, so for user-edited files combine it with a runtime check – for example a schema produced by our YAML to JSONSchema tool.
Typical uses
Typing application settings in Node.js services, reading Markdown front matter in static site generators, building tooling around GitHub Actions or Kubernetes manifests, and documenting the expected shape of a config file for teammates.
Tips
Include several list items when entries differ, so optional properties are detected. Date strings stay string because YAML timestamps are parsed as text by most JavaScript libraries.
Related tools
Validate your input with the YAML Validator, convert it with YAML to JSON, or start from JSON with JSON to TypeScript.
Frequently Asked Questions
How do I load YAML with these types?
Parse the file with a library such as yaml or js-yaml and cast the result, for example const order = parse(text) as Order. For runtime safety, validate it first with a schema library or a JSON Schema.
What happens to null values?
A key whose sample value is null is typed as null, because the real type cannot be inferred; replace it with the intended type such as string | null. If you enable 'null values optional', those keys are also marked with ? so they may be missing entirely.
Should I use interface or type?
Both describe the same shape. Interfaces can be extended and merged; type aliases are handy when you want to combine shapes with unions or intersections. Pick whatever your codebase uses.
Is the YAML processed locally?
Yes. Nothing is uploaded; the parser and generator run in your browser.