JSON to TypeScript Interface
Turn any JSON into clean, nested TypeScript interfaces — in one command. No config needed, works offline, no dependencies.
Why
Pasting an API response and hand-writing types is tedious, and most existing converters either don't handle nested arrays of objects well or bloat everything into any. This one:
- Handles nested objects and arrays properly, including arrays of objects with inconsistent keys (merges shapes, marks missing keys
optional)
- Detects union types when array elements differ (
string | number)
- Marks fields optional when a sample value was
null
- Works on a selection, the whole file, or your clipboard
- Zero runtime dependencies, fully offline
Usage
- Select some JSON in any file (or open a
.json file with nothing selected).
- Right-click → "JSON to TS: Convert Selection to Interfaces", or open the Command Palette (
Ctrl+Shift+P / Cmd+Shift+P) and run one of:
JSON to TS: Convert Selection to Interfaces
JSON to TS: Convert Entire File to Interfaces
JSON to TS: Convert Clipboard to Interfaces
- Get a new
.ts document with generated interfaces (or replace selection / copy to clipboard — see settings).
Example
Input:
{
"id": 1,
"name": "Ada",
"tags": ["dev", "student"],
"address": { "city": "Lagos", "zip": "100001" },
"friends": [{ "id": 2, "name": "Bola", "nickname": "B" }, { "id": 3, "name": "Chi" }]
}
Output:
export interface Address {
city: string;
zip: string;
}
export interface Friend {
id: number;
name: string;
nickname?: string;
}
export interface RootObject {
id: number;
name: string;
tags: string[];
address: Address;
friends: Friend[];
}
Settings
| Setting |
Default |
Description |
jsonToTs.rootInterfaceName |
RootObject |
Name of the top-level generated interface |
jsonToTs.useTypeAlias |
false |
Generate type X = {...} instead of interface X {...} |
jsonToTs.indentSize |
2 |
Spaces per indent level |
jsonToTs.markOptionalOnNull |
true |
Mark a field optional (?) if a sample value was null |
jsonToTs.outputTarget |
newDocument |
newDocument, replaceSelection, or clipboard |
Contributing
Issues and PRs welcome. The core conversion logic lives in extension.js as pure functions (convertJsonToTs), decoupled from the VS Code API, so it's easy to test or reuse elsewhere.
License
MIT