YAML to TypeScript
Turn a YAML sample into typescript interfaces: one type per mapping, nested types named from their field, arrays typed from their first item.
What the YAML to TypeScript does
The YAML is parsed by the shipped js-yaml build, then the value is walked once into a shape: a mapping with named properties, a list with one item shape, or a scalar. Every type declaration is generated from that shape, so a nested object cannot be flattened by accident.
An integer sample becomes an integer type and a fractional one becomes a float, because the sample is the only evidence the page has about the field's type.
The sample is the whole contract: a value that is null in the sample has no type, so it becomes the language's widest type (interface{}, Option<serde_json::Value>, unknown | null, Optional[Any]). A list is typed from its first item only, so a mixed list is typed wrongly.
How to use it
Paste the YAML into the YAML sample box, or press Run with the example already there.
Read the typescript interfaces below: the root mapping is the last type, and every nested mapping appears before it.
Check a nested field in the TypeScript interfaces — its type name comes from the field, so address becomes Address in every language.
Worked example
Input:
user:
name: Ada
age: 36
address:
city: London
tags:
- a
- b
Output:
export interface Address {
city: string;
}
export interface User {
name: string;
age: number;
address: Address;
tags: string[];
}
export interface Root {
user: User;
}
Why it runs in your browser
The result is produced on this page with JavaScript, so it works offline after the page loads and nothing you enter is sent anywhere.
A shape read from the document you paste, printed as typescript interfaces with one exported interface per object.
The root mapping becomes `Root`, a nested `address` mapping becomes `Address`, and a list of mappings becomes `<Field>Item`, so no type borrows the name of the list that holds it.
What it does, and what it does not
The YAML is parsed by the shipped `js-yaml` build, then the value is walked once into a shape: a mapping with named properties, a list with one item shape, or a scalar. Every type declaration is generated from that shape, so a nested object cannot be flattened by accident.
An integer sample becomes an integer type and a fractional one becomes a float, because the sample is the only evidence the page has about the field's type.
The sample is the whole contract: a value that is null in the sample has no type, so it becomes the language's widest type (`interface{}`, `Option<serde_json::Value>`, `unknown | null`, `Optional[Any]`). A list is typed from its first item only, so a mixed list is typed wrongly.
How to use YAML to TypeScript
Paste the YAML into the YAML sample box, or press Run with the example already there.
Read the typescript interfaces below: the root mapping is the last type, and every nested mapping appears before it.
Check a nested field in the TypeScript interfaces — its type name comes from the field, so `address` becomes `Address` in every language.
What to Expect
Best for
- Starting TypeScript types from a real config file
- Seeing how a YAML shape maps to TypeScript
Not ideal for
- Typing a mixed list, which is typed from its first item
- Recovering a comment or an anchor the parser resolved
What this tool keeps
- One type per mapping, named from its field
- Integer and float samples kept distinct
What may need cleanup
- A null sample needs a type chosen by hand
- A mixed list needs a hand-written union
Common errors
- The page refuses it: the YAML root must be a mapping, not a sequence
Example use cases
- Typing a config file in TypeScript
- Checking a nested shape before writing the structs
Sample input
YAML sample: user: …
Sample output
TypeScript interfaces: export interface Address { …
Who this is for
- TypeScript developers reading a YAML config
- Anyone porting a config into typed code
Frequently asked questions
How are nested types named?
From the field that holds them: address becomes Address, and a list of mappings under people becomes PeopleItem, so the list and its element never share a name.
What happens to a null value?
It has no type in the sample, so the page emits the widest one it has rather than guessing. Narrow it by hand where you know what the field holds.
Does it read YAML anchors and aliases?
The shipped parser resolves them, so the shape you get is the expanded document. A recursive alias is refused by the parser rather than looped forever.
Build custom tools for your store
Join the SDK program to create useful ecommerce utilities for your store.