type-flash
v1.6.5
Published
极速智能的 TypeScript 类型生成器,支持深度解析对象 / JSON / 接口返回,一行代码搞定,让类型定义一键生成,彻底告别手写类型的繁琐。
Maintainers
Readme
type-flash
智能 TypeScript 类型生成器 · 零依赖 · 轻量 · 极速
自动生成 TypeScript 类型定义,支持解析对象 / JSON / 接口返回,内置 CLI 命令行工具,一行代码搞定,彻底告别手写类型的繁琐。
功能特性 • 快速开始 • 使用方式 • 配置选项 • 优势对比 • 常见问题
功能特性
type-flash 基于内置的类型推断算法,能够深度解析 JSON 数据结构,自动生成精准的 TypeScript 类型定义。
| 能力 | 说明 | 示例 |
|------|------|------|
| 基础类型识别 | 自动识别 string / number / boolean / null / undefined / function 等多种类型 | name: string |
| 嵌套对象推导 | 递归遍历深层嵌套结构,自动生成子接口定义,支持无限层级 | profile: UserProfile |
| 联合类型合并 | 数组元素类型不一致时,自动合并为联合类型,保留完整类型信息 | (string \| number)[] |
| 可选属性识别 | 智能分析数组中对象的字段出现频率,自动标记可选属性 | desc?: string |
| 结构等价去重 | 基于结构等价性算法,相同结构的对象自动复用同一类型,避免重复定义 | 全局唯一类型 |
| 循环引用检测 | 自动检测 JSON 数据中的循环引用,生成合法的 type alias 处理循环 | 安全无栈溢出 |
| 函数类型支持 | 支持识别函数类型,正确输出 Function 类型声明 | callback: Function |
灵活的输出控制
- 双风格输出:支持
interface和type alias两种输出风格,适配不同团队规范; - 命名风格切换:支持 PascalCase / camelCase 两种命名风格,满足各种编码规范;
- 前后缀自定义:为所有生成的类型名添加统一前缀/后缀,如
IUserDTO; - 自定义类型名:通过
typeNameMap精确指定任意层级字段的类型名称; - 属性排序:支持字母序 / 定义序两种属性排序方式;
工程化能力
| 特性 | 说明 | |------|------| | CLI 命令行 | 开箱即用的命令行工具,支持文件输入/输出、管道输入,轻松集成到工作流 | | 双模块输出 | 同时提供 CommonJS 和 ESM 两种模块格式,兼容 Node.js 和浏览器环境 | | TypeScript 原生 | 纯 TypeScript 编写,类型定义开箱即用,支持 IDE 智能提示 | | 零依赖 | 运行时零第三方依赖,包体仅 ~10KB,轻量无负担 | | 高兼容性 | 支持 Node.js 14+,兼容所有主流 TypeScript 版本 |
快速开始
安装
# npm
npm install type-flash --save-dev
# yarn
yarn add type-flash -D
# 全局安装(使用 CLI)
npm install -g type-flash使用
import { generate } from 'type-flash';
const data = {
id: 1,
name: 'Alice',
email: '[email protected]',
profile: {
avatar: 'https://example.com/avatar.jpg',
bio: 'Hello World',
},
tags: ['admin', 'user', 88],
};
const result = generate(data, { rootName: 'User' });
console.log(result);输出结果:
/**
* Generated by type-flash
* Root type: User
*/
export interface User {
email: string;
id: number;
name: string;
profile: UserProfile;
tags: (string | number)[];
}
export interface UserProfile {
avatar: string;
bio: string;
}使用方式
1. 编程式 API
基础用法
import { generate } from 'type-flash';
const result = generate(data, GenerateOptions);基础示例
import { generate, generateFromString } from 'type-flash';
// 从 JS 对象生成,直接返回代码字符串
const code1 = generate({ name: 'foo', age: 25 }, { rootName: 'User' });
// 从 JSON 字符串生成
const code2 = generateFromString('{"name": "foo"}', { rootName: 'User' });分页数据示例
import { generate } from 'type-flash';
const apiResponse = {
code: 200,
message: 'success',
data: {
list: [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
],
total: 100,
page: 1,
pageSize: 10,
},
};
const code = generate(apiResponse, {
rootName: 'ApiResponse',
});
console.log(code);输出:
export interface ApiResponse {
code: number;
data: ApiResponseData;
message: string;
}
export interface ApiResponseData {
list: ApiResponseDataListItem[];
page: number;
pageSize: number;
total: number;
}
export interface ApiResponseDataListItem {
id: number;
name: string;
}2. CLI 命令行
基础用法
// user.json
{
"id": 1,
"name": "Alice",
"email": "[email protected]",
"profile": {
"avatar": "https://example.com/avatar.jpg",
"bio": "Hello World",
},
"tags": ["admin", "user", 88],
};# 读取文件并生成类型,输出到控制台
type-flash -i user.json
# 指定输出文件
type-flash -i user.json -o user.types.ts
# 自定义根类型名
type-flash -i user.json -n User
# 使用 type 风格输出
type-flash -i user.json -s type常用选项示例
# 添加类型前缀/后缀
type-flash -i user.json --prefix I --suffix DTO -o user.ts
# 不生成 export
type-flash -i user.json --no-export
# 4 空格缩进
type-flash -i user.json --indent 4
# camelCase 命名风格
type-flash -i user.json --naming-style camelCase管道输入(标准输入)
支持通过管道接收其他命令输出的数据,无需新建临时文件,方便接口调试与自动化脚本调用。
适用场景
接口调试:拉取线上接口返回数据即时生成类型,省去存文件步骤;
脚本集成:写自动化脚本时,串联多条命令,自动批量生成类型;
Linux / macOS / Git Bash:
# 从接口直接生成类型
curl https://api.example.com/users | type-flash -n UserList
# 快速测试
echo '{"name": "Alice", "age": 18}' | type-flash -n User
# 从文件读取并管道
cat data.json | type-flash -n UserWindows PowerShell:
# 方式一:先保存到文件(推荐,最稳定)
curl https://api.example.com/users -o temp.json
type-flash -i temp.json -n UserList -o temp.ts
# 方式二:用 curl.exe(需安装 Git 或原生 curl)
curl.exe https://api.example.com/users | type-flash -n UserList
# 方式三:PowerShell 原生方式
(Invoke-RestMethod https://api.example.com/users | ConvertTo-Json -Depth 10) | type-flash -n UserList所有 CLI 选项
| 选项 | 说明 | 默认值 |
|------|------|--------|
| -i, --input <file> | 输入 JSON 文件路径 | - |
| -o, --output <file> | 输出 TS 文件路径 | 控制台输出 |
| -n, --name <name> | 根类型名称 | Root |
| -s, --style <style> | 输出风格: interface | type | interface |
| --naming-style <s> | 命名风格: PascalCase | camelCase | PascalCase |
| --sort <order> | 属性排序: alpha | definition | alpha |
| --no-export | 不添加 export 关键字 | - |
| --no-optional | 不标记可选属性 | - |
| --indent <n> | 缩进空格数 | 2 |
| --prefix <prefix> | 类型名前缀 | - |
| --suffix <suffix> | 类型名后缀 | - |
| -h, --help | 显示帮助信息 | - |
| -v, --version | 显示版本号 | - |
配置选项
GenerateOptions 完整配置表
| 参数 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| rootName | string | 'Root' | 根类型名称 |
| outputStyle | 'interface' \| 'type' | 'interface' | 输出风格,接口或类型别名 |
| namingStyle | 'PascalCase' \| 'camelCase' | 'PascalCase' | 生成的类型名命名风格 |
| sortProperties | 'alpha' \| 'definition' | 'alpha' | 属性排序方式,字母序或定义序 |
| addExport | boolean | true | 是否添加 export 关键字 |
| markOptional | boolean | true | 是否自动标记可选属性 |
| indentSize | number | 2 | 缩进空格数 |
| lineEnding | string | '\n' | 行尾换行符 |
| typeNameMap | Record<string, string> | {} | 自定义类型名映射,key 为字段路径 |
| typePrefix | string | '' | 所有类型名添加此前缀 |
| typeSuffix | string | '' | 所有类型名添加此后缀 |
自定义类型名映射
const code = generate(jsonData, {
rootName: 'ApiResponse',
typeNameMap: {
'ApiResponse.data': 'ResponseData',
'ApiResponse.data.list': 'UserItem',
'ApiResponse.data.list.extra': 'ExtraInfo',
},
});💡 提示:
typeNameMap的 key 格式为{根类型名}.{字段名}.{子字段名}...,支持任意深度。
优势对比
| 特性 | type-flash | json-schema-to-typescript | 其它 | |------|-----------|---------------------------|--------------| | 直接输入 JSON | ✅ | ❌ 需要 JSON Schema | ✅ | | 嵌套对象智能命名 | ✅ 上下文感知命名 | ⚠️ 简单拼接 | ⚠️ 简单命名 | | 联合类型推断 | ✅ 完整支持 | ⚠️ 有限支持 | ❌ | | 可选属性识别 | ✅ 频率分析 | ✅ | ❌ | ⚠️部分 | | 结构等价去重 | ✅ 精确算法 | ⚠️ 有限 | ⚠️部分 | | 循环引用处理 | ✅ 安全检测 | ✅ | ❌ | | 函数类型支持 | ✅ | ❌ | ❌ | | CLI 命令行 | ✅ | ✅ | ❌ | | 编程式 API | ✅ | ✅ | ❌ | | 零运行时依赖 | ✅ | ❌ 依赖多 | ❌ 包体大 | | 包体大小 | ~10KB | ~100KB | ~500KB | | 安装速度 | ⚡ 极快 | 🐢 较慢 | 🐢 慢 |
常见问题
Q: 生成的类型名为什么是 UserProfile 而不是 Profile?
A: type-flash 默认会用父级类型名 + 字段名来生成子类型名,避免重名。如果想自定义类型名,可以使用 typeNameMap 配置。
Q: 支持循环引用的 JSON 吗?
A: 支持。type-flash 会自动检测循环引用并生成合法的 type alias,不会出现栈溢出。
Q: 可以在浏览器环境使用吗?
A: 可以。type-flash 同时提供 ESM 格式,支持在浏览器和 Node.js 环境中使用。
如果觉得好用,别忘了给个 ⭐ Star 支持一下喔~
