type-flash 1.1.10 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +186 -150
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -2,46 +2,55 @@
2
2
 
3
3
  # type-flash
4
4
 
5
- **JSON 智能生成 TypeScript 类型定义**
5
+ **智能 TypeScript 类型生成器 · 零依赖 · 轻量 · 极速**
6
6
 
7
7
  [![npm version](https://img.shields.io/npm/v/type-flash.svg?style=flat-square)](https://www.npmjs.com/package/type-flash)
8
8
  [![license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)
9
9
 
10
- > **One line of code, types generated instantly.**
11
- > 一行代码,类型秒出。告别手写 Interface 的繁琐时代。
12
10
 
13
- [功能特性](#-功能特性) [快速开始](#-快速开始) [使用方式](#-使用方式) [配置选项](#-配置选项) • [对比优势](#-对比优势)
11
+ > 自动生成 TypeScript 类型定义,支持对象 / JSON / 接口返回,一行代码搞定,彻底告别手写类型的繁琐工作。
12
+
13
+ [功能特性](#功能特性) • [快速开始](#快速开始) • [使用方式](#使用方式) • [配置选项](#配置选项) • [优势对比](#优势对比) • [常见问题](#常见问题)
14
14
 
15
15
  </div>
16
16
 
17
17
  ---
18
18
 
19
- ## 功能特性
19
+ ## 功能特性
20
20
 
21
- ### 🎯 核心能力
21
+ type-flash 基于内置的类型推断算法,能够深度解析 JSON 数据结构,自动生成精准的 TypeScript 类型定义。
22
22
 
23
- - **🔍 智能类型推断** - 自动识别 string / number / boolean / null / undefined 等基础类型
24
- - **📦 嵌套对象支持** - 递归遍历深层结构,自动生成子接口定义
25
- - **🔗 联合类型推断** - 数组元素类型不同?自动合并为 `(string | number)[]`
26
- - **❓ 可选属性识别** - 数组中部分对象缺少某字段?自动标记为 `?` 可选
27
- - **🔄 结构等价去重** - 相同结构的对象复用同一类型,避免重复定义
28
- - **🔁 循环引用检测** - 智能检测并处理 JSON 中的循环引用
23
+ | 能力 | 说明 | 示例 |
24
+ |------|------|------|
25
+ | **基础类型识别** | 自动识别 string / number / boolean / null / undefined / function 等多种类型 | `name: string` |
26
+ | **嵌套对象推导** | 递归遍历深层嵌套结构,自动生成子接口定义,支持无限层级 | `profile: UserProfile` |
27
+ | **联合类型合并** | 数组元素类型不一致时,自动合并为联合类型,保留完整类型信息 | `(string \| number)[]` |
28
+ | **可选属性识别** | 智能分析数组中对象的字段出现频率,自动标记可选属性 | `desc?: string` |
29
+ | **结构等价去重** | 基于结构等价性算法,相同结构的对象自动复用同一类型,避免重复定义 | 全局唯一类型 |
30
+ | **循环引用检测** | 自动检测 JSON 数据中的循环引用,生成合法的 type alias 处理循环 | 安全无栈溢出 |
31
+ | **函数类型支持** | 支持识别函数类型,正确输出 Function 类型声明 | `callback: Function` |
29
32
 
30
- ### ⚡ 增强功能
33
+ #### 灵活的输出控制
31
34
 
32
- - **🧬 泛型模式识别** - 智能识别分页结构 `{ list: T[], total: number }`,提取为 `PageResult<T>`
33
- - **🎨 多种输出风格** - 支持 `interface` 和 `type alias` 两种输出风格
34
- - **📏 灵活命名控制** - 支持 PascalCase / camelCase、前缀后缀、自定义类型名映射
35
+ - **双风格输出**:支持 `interface` `type alias` 两种输出风格,适配不同团队规范;
36
+ - **命名风格切换**:支持 PascalCase / camelCase 两种命名风格,满足各种编码规范;
37
+ - **前后缀自定义**:为所有生成的类型名添加统一前缀/后缀,如 `IUserDTO`;
38
+ - **自定义类型名**:通过 `typeNameMap` 精确指定任意层级字段的类型名称;
39
+ - **属性排序**:支持字母序 / 定义序两种属性排序方式;
35
40
 
36
- ### 🛠️ 工程化支持
41
+ #### 工程化能力
37
42
 
38
- - **💻 CLI 命令行工具** - 开箱即用的命令行工具,支持管道输入
39
- - **📦 双模块输出** - 同时提供 CommonJS 和 ESM 两种模块格式
40
- - **🔩 TypeScript 原生** - 纯 TypeScript 编写,类型定义开箱即用
43
+ | 特性 | 说明 |
44
+ |------|------|
45
+ | **CLI 命令行** | 开箱即用的命令行工具,支持文件输入/输出、管道输入,轻松集成到工作流 |
46
+ | **双模块输出** | 同时提供 CommonJS 和 ESM 两种模块格式,兼容 Node.js 和浏览器环境 |
47
+ | **TypeScript 原生** | 纯 TypeScript 编写,类型定义开箱即用,支持 IDE 智能提示 |
48
+ | **零依赖** | 运行时零第三方依赖,包体仅 ~10KB,轻量无负担 |
49
+ | **高兼容性** | 支持 Node.js 14+,兼容所有主流 TypeScript 版本 |
41
50
 
42
51
  ---
43
52
 
44
- ## 🚀 快速开始
53
+ ## 快速开始
45
54
 
46
55
  ### 安装
47
56
 
@@ -52,16 +61,16 @@ npm install type-flash --save-dev
52
61
  # yarn
53
62
  yarn add type-flash -D
54
63
 
55
- # pnpm
56
- pnpm add type-flash -D
64
+ # 全局安装(使用 CLI)
65
+ npm install -g type-flash
57
66
  ```
58
67
 
59
- ### 一行代码上手
68
+ ### 使用
60
69
 
61
70
  ```typescript
62
71
  import { generate } from 'type-flash';
63
72
 
64
- const jsonData = {
73
+ const data = {
65
74
  id: 1,
66
75
  name: 'Alice',
67
76
  email: 'alice@example.com',
@@ -69,11 +78,11 @@ const jsonData = {
69
78
  avatar: 'https://example.com/avatar.jpg',
70
79
  bio: 'Hello World',
71
80
  },
72
- tags: ['admin', 'user'],
81
+ tags: ['admin', 'user', 88],
73
82
  };
74
83
 
75
- const result = generate(jsonData, { rootName: 'User' });
76
- console.log(result.code);
84
+ const result = generate(data, { rootName: 'User' });
85
+ console.log(result);
77
86
  ```
78
87
 
79
88
  **输出结果:**
@@ -83,13 +92,12 @@ console.log(result.code);
83
92
  * Generated by type-flash
84
93
  * Root type: User
85
94
  */
86
-
87
95
  export interface User {
88
96
  email: string;
89
97
  id: number;
90
98
  name: string;
91
99
  profile: UserProfile;
92
- tags: string[];
100
+ tags: (string | number)[];
93
101
  }
94
102
 
95
103
  export interface UserProfile {
@@ -100,40 +108,42 @@ export interface UserProfile {
100
108
 
101
109
  ---
102
110
 
103
- ## 📖 使用方式
111
+ ## 使用方式
104
112
 
105
113
  ### 1. 编程式 API
106
114
 
107
115
  #### 基础用法
108
116
 
109
117
  ```typescript
110
- import { generate, generateFromString } from 'type-flash';
118
+ import { generate } from 'type-flash';
111
119
 
112
- // JS 对象生成
113
- const result1 = generate({ name: 'foo', age: 25 }, { rootName: 'User' });
120
+ const result = generate(data, GenerateOptions);
121
+ ```
114
122
 
115
- // 从 JSON 字符串生成
116
- const result2 = generateFromString('{"name": "foo"}', { rootName: 'User' });
123
+ #### 基础示例
117
124
 
118
- // 获取生成的代码
119
- console.log(result1.code);
125
+ ```typescript
126
+ import { generate, generateFromString } from 'type-flash';
127
+
128
+ // 从 JS 对象生成,直接返回代码字符串
129
+ const code1 = generate({ name: 'foo', age: 25 }, { rootName: 'User' });
120
130
 
121
- // 获取所有类型定义
122
- console.log(result1.types);
131
+ // 从 JSON 字符串生成
132
+ const code2 = generateFromString('{"name": "foo"}', { rootName: 'User' });
123
133
  ```
124
134
 
125
- #### 分页接口示例
135
+ #### 分页数据示例
126
136
 
127
137
  ```typescript
128
138
  import { generate } from 'type-flash';
129
139
 
130
140
  const apiResponse = {
131
- code: 0,
141
+ code: 200,
132
142
  message: 'success',
133
143
  data: {
134
144
  list: [
135
- { id: 1, name: 'Alice', status: 'active' },
136
- { id: 2, name: 'Bob', status: 'inactive' },
145
+ { id: 1, name: 'Alice' },
146
+ { id: 2, name: 'Bob' },
137
147
  ],
138
148
  total: 100,
139
149
  page: 1,
@@ -141,15 +151,14 @@ const apiResponse = {
141
151
  },
142
152
  };
143
153
 
144
- const result = generate(apiResponse, {
154
+ const code = generate(apiResponse, {
145
155
  rootName: 'ApiResponse',
146
- extractGenerics: true,
147
156
  });
148
157
 
149
- console.log(result.code);
158
+ console.log(code);
150
159
  ```
151
160
 
152
- **输出:**
161
+ 输出:
153
162
 
154
163
  ```typescript
155
164
  export interface ApiResponse {
@@ -159,13 +168,13 @@ export interface ApiResponse {
159
168
  }
160
169
 
161
170
  export interface ApiResponseData {
162
- list: PageResult<ListItem>;
171
+ list: ApiResponseDataListItem[];
163
172
  page: number;
164
173
  pageSize: number;
165
174
  total: number;
166
175
  }
167
176
 
168
- export interface ListItem {
177
+ export interface ApiResponseDataListItem {
169
178
  id: number;
170
179
  name: string;
171
180
  }
@@ -175,34 +184,88 @@ export interface ListItem {
175
184
 
176
185
  #### 基础用法
177
186
 
187
+ ```json
188
+ // user.json
189
+ {
190
+ "id": 1,
191
+ "name": 'Alice',
192
+ "email": 'alice@example.com',
193
+ "profile": {
194
+ "avatar": 'https://example.com/avatar.jpg',
195
+ "bio": 'Hello World',
196
+ },
197
+ "tags": ['admin', 'user', 88],
198
+ };
199
+ ```
200
+
178
201
  ```bash
179
- # 从文件生成,输出到控制台
180
- type-flash user.json
202
+ # 读取文件并生成类型,输出到控制台
203
+ type-flash -i user.json
181
204
 
182
205
  # 指定输出文件
183
- type-flash user.json -o user.types.ts
206
+ type-flash -i user.json -o user.types.ts
184
207
 
185
208
  # 自定义根类型名
186
- type-flash user.json -n User
209
+ type-flash -i user.json -n User
187
210
 
188
211
  # 使用 type 风格输出
189
- type-flash data.json --style type
212
+ type-flash -i user.json -s type
190
213
  ```
191
214
 
192
- #### 高级用法
215
+ #### 常用选项示例
193
216
 
194
217
  ```bash
195
- # 从标准输入读取(管道)
218
+ # 严格空值模式(保留 null 为独立类型)
219
+ type-flash -i user.json --strict-null
220
+
221
+ # 添加类型前缀/后缀
222
+ type-flash -i user.json --prefix I --suffix DTO -o user.ts
223
+
224
+ # 不生成 export
225
+ type-flash -i user.json --no-export
226
+
227
+ # 4 空格缩进
228
+ type-flash -i user.json --indent 4
229
+
230
+ # camelCase 命名风格
231
+ type-flash -i user.json --naming-style camelCase
232
+ ```
233
+
234
+ #### 管道输入(标准输入)
235
+
236
+ 支持通过管道接收其他命令输出的数据,无需新建临时文件,方便接口调试与自动化脚本调用。
237
+
238
+ > **适用场景**
239
+ >
240
+ > - 接口调试:拉取线上接口返回数据即时生成类型,省去存文件步骤;
241
+ >
242
+ > - 脚本集成:写自动化脚本时,串联多条命令,自动批量生成类型;
243
+ >
244
+ >
245
+
246
+ **Linux / macOS / Git Bash:**
247
+ ```bash
248
+ # 从接口直接生成类型
196
249
  curl https://api.example.com/users | type-flash -n UserList
197
250
 
198
- # 严格空值模式
199
- type-flash data.json --strict-null
251
+ # 快速测试
252
+ echo '{"name": "Alice", "age": 18}' | type-flash -n User
253
+
254
+ # 从文件读取并管道
255
+ cat data.json | type-flash -n User
256
+ ```
200
257
 
201
- # 添加类型前缀
202
- type-flash response.json --prefix I -o interfaces.ts
258
+ **Windows PowerShell:**
259
+ ```powershell
260
+ # 方式一:先保存到文件(推荐,最稳定)
261
+ curl https://api.example.com/users -o temp.json
262
+ type-flash -i temp.json -n UserList -o temp.ts
203
263
 
204
- # 不生成 export
205
- type-flash types.json --no-export
264
+ # 方式二:用 curl.exe(需安装 Git 或原生 curl)
265
+ curl.exe https://api.example.com/users | type-flash -n UserList
266
+
267
+ # 方式三:PowerShell 原生方式
268
+ (Invoke-RestMethod https://api.example.com/users | ConvertTo-Json -Depth 10) | type-flash -n UserList
206
269
  ```
207
270
 
208
271
  #### 所有 CLI 选项
@@ -210,120 +273,93 @@ type-flash types.json --no-export
210
273
  | 选项 | 说明 | 默认值 |
211
274
  |------|------|--------|
212
275
  | `-i, --input <file>` | 输入 JSON 文件路径 | - |
213
- | `-o, --output <file>` | 输出 TS 文件路径 | stdout |
276
+ | `-o, --output <file>` | 输出 TS 文件路径 | 控制台输出 |
214
277
  | `-n, --name <name>` | 根类型名称 | `Root` |
215
278
  | `-s, --style <style>` | 输出风格: `interface` \| `type` | `interface` |
216
279
  | `--naming-style <s>` | 命名风格: `PascalCase` \| `camelCase` | `PascalCase` |
217
280
  | `--sort <order>` | 属性排序: `alpha` \| `definition` | `alpha` |
218
281
  | `--no-export` | 不添加 export 关键字 | - |
219
- | `--strict-null` | 严格空值模式 | - |
282
+ | `--strict-null` | 严格空值模式(保留 null 类型) | - |
220
283
  | `--no-optional` | 不标记可选属性 | - |
221
- | `--no-generics` | 不提取泛型类型 | - |
222
284
  | `--indent <n>` | 缩进空格数 | `2` |
223
285
  | `--prefix <prefix>` | 类型名前缀 | - |
224
286
  | `--suffix <suffix>` | 类型名后缀 | - |
225
- | `-h, --help` | 显示帮助 | - |
287
+ | `-h, --help` | 显示帮助信息 | - |
226
288
  | `-v, --version` | 显示版本号 | - |
227
289
 
228
290
  ---
229
291
 
230
- ## ⚙️ 配置选项
231
-
232
- ### GenerateOptions 完整配置
233
-
234
- ```typescript
235
- interface GenerateOptions {
236
- /** 根类型名称,默认 'Root' */
237
- rootName?: string;
238
-
239
- /** 输出风格:interface type,默认 'interface' */
240
- outputStyle?: 'interface' | 'type';
241
-
242
- /** 命名风格:PascalCase camelCase,默认 'PascalCase' */
243
- namingStyle?: 'PascalCase' | 'camelCase';
244
-
245
- /** 属性排序方式,默认 'alpha' */
246
- sortProperties?: 'alpha' | 'definition';
247
-
248
- /** 是否添加 export 语句,默认 true */
249
- addExport?: boolean;
250
-
251
- /** 是否严格空值,默认 false */
252
- strictNullChecks?: boolean;
253
-
254
- /** 是否标记可选属性,默认 true */
255
- markOptional?: boolean;
256
-
257
- /** 是否提取泛型,默认 true */
258
- extractGenerics?: boolean;
259
-
260
- /** 缩进空格数,默认 2 */
261
- indentSize?: number;
262
-
263
- /** 自定义类型名映射 */
264
- typeNameMap?: Record<string, string>;
265
-
266
- /** 类型名前缀 */
267
- typePrefix?: string;
268
-
269
- /** 类型名后缀 */
270
- typeSuffix?: string;
271
- }
272
- ```
273
-
274
- ### 配置示例
275
-
276
- #### 企业级 DTO 配置
277
-
278
- ```typescript
279
- import { generate } from 'type-flash';
280
-
281
- const result = generate(jsonData, {
282
- rootName: 'UserDTO',
283
- outputStyle: 'interface',
284
- namingStyle: 'PascalCase',
285
- typePrefix: 'I',
286
- typeSuffix: 'DTO',
287
- addExport: true,
288
- sortProperties: 'alpha',
289
- extractGenerics: true,
290
- strictNullChecks: true,
291
- indentSize: 2,
292
- });
293
- ```
292
+ ## 配置选项
293
+
294
+ ### GenerateOptions 完整配置表
295
+
296
+ | 参数 | 类型 | 默认值 | 说明 |
297
+ |------|------|--------|------|
298
+ | `rootName` | `string` | `'Root'` | 根类型名称 |
299
+ | `outputStyle` | `'interface' \| 'type'` | `'interface'` | 输出风格,接口或类型别名 |
300
+ | `namingStyle` | `'PascalCase' \| 'camelCase'` | `'PascalCase'` | 生成的类型名命名风格 |
301
+ | `sortProperties` | `'alpha' \| 'definition'` | `'alpha'` | 属性排序方式,字母序或定义序 |
302
+ | `addExport` | `boolean` | `true` | 是否添加 export 关键字 |
303
+ | `strictNullChecks` | `boolean` | `false` | 是否启用严格空值模式,保留 null 为独立类型 |
304
+ | `markOptional` | `boolean` | `true` | 是否自动标记可选属性 |
305
+ | `indentSize` | `number` | `2` | 缩进空格数 |
306
+ | `lineEnding` | `string` | `'\n'` | 行尾换行符 |
307
+ | `typeNameMap` | `Record<string, string>` | `{}` | 自定义类型名映射,key 为字段路径 |
308
+ | `typePrefix` | `string` | `''` | 所有类型名添加此前缀 |
309
+ | `typeSuffix` | `string` | `''` | 所有类型名添加此后缀 |
294
310
 
295
311
  #### 自定义类型名映射
296
312
 
297
313
  ```typescript
298
- const result = generate(jsonData, {
314
+ const code = generate(jsonData, {
299
315
  rootName: 'ApiResponse',
300
316
  typeNameMap: {
301
317
  'ApiResponse.data': 'ResponseData',
302
- 'ResponseData.list': 'UserItem',
318
+ 'ApiResponse.data.list': 'UserItem',
319
+ 'ApiResponse.data.list.extra': 'ExtraInfo',
303
320
  },
304
321
  });
305
322
  ```
306
323
 
324
+ > 💡 **提示**:`typeNameMap` 的 key 格式为 `{根类型名}.{字段名}.{子字段名}...`,支持任意深度。
325
+
307
326
  ---
308
327
 
309
- ## 🆚 对比优势
328
+ ## 优势对比
310
329
 
311
- ### 与其他方案对比
312
330
 
313
- | 特性 | type-flash | json-schema-to-typescript | 在线转换工具 | quicktype |
314
- |------|--------|---------------------------|--------------|-----------|
315
- | **直接输入 JSON** | ✅ | ❌ 需要 JSON Schema | ✅ | ✅ |
316
- | **嵌套对象自动命名** | ✅ 智能命名 | | ⚠️ 简单命名 | ✅ |
317
- | **联合类型推断** | ✅ | ⚠️ 有限支持 | ❌ | ✅ |
318
- | **可选属性识别** | ✅ | ✅ | ❌ | |
319
- | **枚举自动提取** | ✅ | ⚠️ 需手动定义 | | ✅ |
320
- | **泛型模式识别** | ✅ | | | ❌ |
321
- | **结构等价去重** | ✅ | ⚠️ 有限 | ❌ | ✅ |
322
- | **循环引用处理** | ✅ | ✅ | ❌ | ✅ |
323
- | **CLI 命令行** | ✅ | ✅ | ❌ | ✅ |
324
- | **编程式 API** | ✅ | | ❌ | |
325
- | **零依赖** | | 依赖多 | - | ❌ 包体大 |
326
- | **体积大小** | ~10KB | ~100KB | - | ~500KB |
331
+ | 特性 | type-flash | json-schema-to-typescript | 其它 |
332
+ |------|-----------|---------------------------|--------------|
333
+ | **直接输入 JSON** | ✅ | ❌ 需要 JSON Schema | ✅ |
334
+ | **嵌套对象智能命名** | ✅ 上下文感知命名 | ⚠️ 简单拼接 | ⚠️ 简单命名 |
335
+ | **联合类型推断** | ✅ 完整支持 | ⚠️ 有限支持 | ❌ |
336
+ | **可选属性识别** | ✅ 频率分析 | ✅ | ❌ | ⚠️部分 |
337
+ | **结构等价去重** | ✅ 精确算法 | ⚠️ 有限 | ⚠️部分 |
338
+ | **循环引用处理** | ✅ 安全检测 | | ❌ |
339
+ | **函数类型支持** | ✅ | | ❌ |
340
+ | **CLI 命令行** | ✅ | ✅ | ❌ |
341
+ | **编程式 API** | ✅ | ✅ | ❌ |
342
+ | **零运行时依赖** | ✅ | 依赖多 | ❌ 包体大 |
343
+ | **包体大小** | ~10KB | ~100KB | ~500KB |
344
+ | **安装速度** | 极快 | 🐢 较慢 | 🐢 |
345
+
346
+ ---
347
+
348
+ ## 常见问题
349
+
350
+ ### Q: 生成的类型名为什么是 UserProfile 而不是 Profile?
351
+ A: type-flash 默认会用父级类型名 + 字段名来生成子类型名,避免重名。如果想自定义类型名,可以使用 `typeNameMap` 配置。
352
+
353
+ ### Q: 为什么数组里的 null 被去掉了?
354
+ A: 默认情况下 `strictNullChecks` 为 `false`,会自动移除 null 类型。如果需要保留 null,可以设置 `strictNullChecks: true`。
355
+
356
+ ### Q: 支持循环引用的 JSON 吗?
357
+ A: 支持。type-flash 会自动检测循环引用并生成合法的 type alias,不会出现栈溢出。
358
+
359
+ ### Q: 可以在浏览器环境使用吗?
360
+ A: 可以。type-flash 同时提供 ESM 格式,支持在浏览器和 Node.js 环境中使用。
361
+
362
+ ---
327
363
 
328
- **如果觉得好用,别忘了给个 ⭐ Star 支持一下!**
329
364
 
365
+ **如果觉得好用,别忘了给个 ⭐ Star 支持一下喔~**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-flash",
3
- "version": "1.1.10",
3
+ "version": "1.2.0",
4
4
  "description": "JSON 智能生成 TypeScript 类型定义 - 一键从 JSON 数据生成 Interface / Type 定义",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",