type-flat

npm version License: Apache

**TypeScript Type Flattening Tool Recursively parses complex types and generates nested JSON or type declarations.**

ðŸ§Đ Introduction

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:


🚀 Features


🛠ïļ Installation

npm install type-flat
# or
pnpm add type-flat

🧑‍ðŸ’ŧ CLI Usage

npx type-flat <file> <typeName>

Example

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"
      }
    }
  }
}

🧑‍ðŸ’ŧ Programming Interface

import { flatten } from 'type-flat';
import Content from './types.d.ts'

const result = await flatten(Content, 'ResponseOfUser');
console.log(JSON.stringify(result, null, 2));

Cross-file Generic Example

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"
      }
    }
  }
}

ðŸ§ą Use Cases


ðŸ“Ķ Output Format


ðŸŠī License

Apache-2.0 ÂĐ 2025 hboot