Skip to content

YAML to TypeScript Converter

Convert YAML to TypeScript online, for free. This tool infers a TypeScript interface or type alias directly from the shape of your YAML data, whether it's saved as .yaml or .yml — handy for typing configuration files or API fixtures without writing types by hand.

Drop your YAML file here

or

Supports .yaml and .yml files, processed entirely on your device

Untitled.yaml0 B0 lines0 characters
YAML →JSON
Your converted output will appear here.
Waiting for YAML input.

What to expect

  • Nested mappings become nested inline object types.
  • Lists of objects produce a RootItem interface plus a Root array type.
  • Mixed-type arrays are typed as a union of the observed element types.

Example

Input (YAML)
site:
  name: Aperture Docs
  version: 1.4.2
  active: true
tags:
  - web
  - cli
  - tools
author:
  name: Jane Doe
  email: jane@example.com
Output (typescript)
export interface Root {
  site: {
    name: string;
    version: string;
    active: boolean;
  };
  tags: string[];
  author: {
    name: string;
    email: string;
  };
}

Generating types from your YAML

Loading a YAML config into a TypeScript project usually leaves you with the any type, which defeats the point of using TypeScript. Writing an interface by hand for a large file is tedious and easy to get wrong. This tool reads the structure of your YAML and infers an interface for it, so you can start with accurate types and refine from there.

The inference is based on the sample you paste in. It sees one example of your data, so it can tell you what shape that example has, but it cannot know which fields are always present. Treat the output as a strong first draft and tighten it using your real schema where it matters.

  • Typing config files

    Get an interface for a YAML config instead of casting it to any.

  • API fixtures and mock data

    Type the sample data your tests or Storybook stories load.

  • Documenting data shapes

    Share a precise description of a YAML structure with teammates.

  • Migrating to TypeScript

    Speed up converting an untyped project that reads YAML at startup.

How YAML maps to TypeScript

These are the exact rules this converter applies. Each result below was generated by the tool itself, so it matches what you will see in the output panel.

Scalars become primitive types

Strings, numbers and booleans map directly to their TypeScript equivalents.

YAML
name: Ada
age: 36
admin: false
TypeScript
export interface Root {
  name: string;
  age: number;
  admin: boolean;
}

Null makes a field optional

A null value is typed as optional, since one sample can't prove the field is always present.

YAML
nickname: null
city: Paris
TypeScript
export interface Root {
  nickname?: null;
  city: string;
}

Arrays are typed by their items

Uniform arrays get a simple array type, and mixed ones become a union of the observed types.

YAML
ids: [1, 2]
mixed: [1, a]
TypeScript
export interface Root {
  ids: number[];
  mixed: (number | string)[];
}

A top-level list creates two types

You get an item interface for one record plus a Root array type built from it.

YAML
- id: 1
  title: Intro
- id: 2
  title: Setup
TypeScript
export interface RootItem {
  id: number;
  title: string;
}

export type Root = RootItem[];

A trickier example: odd key names and mixed values

Keys with hyphens or spaces aren't valid TypeScript identifiers, and arrays can hold more than one kind of value.

Input (YAML)
user-id: 42
display_name: Ada
nickname: null
scores: [10, 20.5, high]
meta:
  created: 2026-01-01
  "api key": abc
Output (TypeScript)
export interface Root {
  "user-id": number;
  display_name: string;
  nickname?: null;
  scores: (number | string)[];
  meta: {
    created: string;
    "api key": string;
  };
}

Keys that aren't valid identifiers are quoted, the null nickname becomes optional, and the mixed scores array is typed as a union. Dates in YAML are read as text here, so created is typed as a string.

Conversion notes for this example
  • Arrays with mixed item types were typed as a union of the observed element types.

Using the generated type

Save the output as a types file, then assert your parsed YAML against it. This example uses the popular yaml package:

typescript
import { readFileSync } from "node:fs";
import { parse } from "yaml";
import type { Root } from "./config.types";

const config = parse(readFileSync("config.yaml", "utf8")) as Root;

console.log(config.site.name);

YAML vs TypeScript at a glance

A YAML file holds data; a TypeScript type describes its shape.

FeatureYAMLTypeScript
PurposeStores valuesDescribes the shape of values
Checked whenAt runtime, by your parserAt compile time, by tsc
Optional fieldsJust omit the keyMarked with ?
UnionsNot expressedstring | number
Runtime costMust be parsedErased; no runtime output

Things to watch for when converting to TypeScript

  • Types come from a single sample, so fields that are sometimes missing will be marked required unless the sample had them as null.
  • Casting parsed YAML with 'as' trusts the types; it does not validate the data at runtime.
  • A list of differently shaped items generates one interface per shape plus a union, which can look surprising at first.
  • Rename Root and RootItem to something meaningful before committing the types to your codebase.

YAML to TypeScript Converter FAQ

Does this infer optional fields?

Fields whose YAML value is null are marked optional with a `?`, since the field's presence can't be confirmed from a single sample.

What if my YAML is a list at the top level?

The tool generates a RootItem interface from the first item's shape, then exports Root as RootItem[].

Can I rename the generated interface?

Yes, copy the output and rename "Root" or "RootItem" to whatever fits your codebase.

Is the generated type checked against my data at runtime?

No. TypeScript types are erased at compile time. Use a validation library such as Zod alongside the interface if you also need runtime checks on loaded YAML.

Why is a field marked optional?

When a value is null in your sample, the converter can't tell whether the field is meant to be present. It adds a question mark so the type allows the field to be missing.

Interface or type alias?

Objects and lists of objects are emitted as interfaces. A top-level scalar or a list of non-objects is emitted as a type alias, because an interface can only describe an object shape.

Is there a difference between .yml and .yaml?

No. ".yml" and ".yaml" are just two file extensions for the exact same format — this converter accepts either one, so a "yml to TypeScript" conversion and a "yaml to TypeScript" conversion produce identical results.