Why Type YAML in TypeScript?
YAML is everywhere in modern development: Kubernetes configs, Docker Compose, GitHub Actions workflows, Helm values files, application configs. When you read these files in TypeScript, you get any by default — which means no autocomplete, no type checking, and runtime errors when the YAML structure changes.
Typing your YAML configurations gives you compile-time validation, autocomplete when building config files programmatically, and immediate feedback when a config schema changes.
Quick Conversion: Online Tool
For one-off conversions, use our YAML to TypeScript converter. Paste your YAML and get TypeScript interfaces generated automatically — handles nested objects, arrays, optional fields, and all YAML scalar types.
YAML to TypeScript Type Mapping
Understanding the mapping:
# YAML types → TypeScript types
string: "hello" # → string
number: 42 # → number
float: 3.14 # → number
boolean: true # → boolean
null: ~ # → null
array: # → T[]
- item1
- item2
nested: # → nested interface
key: valueKubernetes Manifest Types
Here's a Kubernetes Deployment manifest and its TypeScript interface:
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:latest
ports:
- containerPort: 3000// TypeScript interfaces
export interface KubernetesMetadata {
name: string;
labels?: Record<string, string>;
namespace?: string;
}
export interface ContainerPort {
containerPort: number;
protocol?: string;
}
export interface Container {
name: string;
image: string;
ports?: ContainerPort[];
env?: Array<{ name: string; value: string }>;
}
export interface DeploymentSpec {
replicas: number;
selector: { matchLabels: Record<string, string> };
template: {
metadata: { labels: Record<string, string> };
spec: { containers: Container[] };
};
}
export interface Deployment {
apiVersion: string;
kind: 'Deployment';
metadata: KubernetesMetadata;
spec: DeploymentSpec;
}Reading Typed YAML in Node.js
import { readFileSync } from 'fs';
import { parse } from 'yaml';
import { z } from 'zod';
// Define schema for validation
const ConfigSchema = z.object({
database: z.object({
host: z.string(),
port: z.number().int(),
name: z.string(),
}),
server: z.object({
port: z.number().int(),
cors: z.boolean().optional(),
}),
});
type Config = z.infer<typeof ConfigSchema>;
function loadConfig(path: string): Config {
const raw = readFileSync(path, 'utf-8');
const parsed: unknown = parse(raw);
return ConfigSchema.parse(parsed); // throws if invalid
}GitHub Actions Workflow Typing
When building tools that process GitHub Actions workflows, type the YAML structure:
export interface WorkflowJob {
name?: string;
'runs-on': string | string[];
needs?: string | string[];
steps: Array<{
name?: string;
uses?: string;
run?: string;
with?: Record<string, string | number | boolean>;
env?: Record<string, string>;
}>;
}
export interface Workflow {
name?: string;
on: string | string[] | Record<string, unknown>;
jobs: Record<string, WorkflowJob>;
}For a live YAML-to-TypeScript converter, visit js2ts.com/yaml-to-typescript.
