| **TypeScript Type Flattening Tool | Recursively parses complex types and generates nested JSON or type declarations.** |
type-flat is a TypeScript type flattening tool that recursively parses complex types, including generics, nested objects, and intersection types, and generates structured nested type definitions. It is suitable for:
Response<User>.d.ts type declarationsnpm install type-flat
# or
pnpm add type-flat
npx type-flat <file> <typeName>
<file>: Path to the TypeScript file (.ts or .d.ts)<typeName>: Type or interface name to flattenExample
npx type-flat -f example/types.ts -t ResponseOfUser
Output:
{
"code": "number",
"message": "string",
"data": {
"id": "number",
"name": "string",
"profile": {
"email": "string",
"address": {
"city": "string",
"zip": "number"
}
}
}
}
import { flatten } from 'type-flat';
import Content from './types.d.ts'
const result = await flatten(Content, 'ResponseOfUser');
console.log(JSON.stringify(result, null, 2));
types.ts:
export interface Address { city: string; zip: number; }
export interface Profile { email: string; address: Address; }
export interface User { id: number; name: string; profile: Profile; }
export interface Response<T> { code: number; message: string; data: T; }
export type ResponseOfUser = Response<User>;
Usage:
import { flatten } from 'type-flat';
const res = await flatten(Content, 'ResponseOfUser');
console.log(res);
Output:
{
"code": "number",
"message": "string",
"data": {
"id": "number",
"name": "string",
"profile": {
"email": "string",
"address": {
"city": "string",
"zip": "number"
}
}
}
}
.d.ts filesApache-2.0 ÂĐ 2025 hboot