feat: add TypeScript type definition generation for csv-loader
- Add emitTypes option (default true) to generate .d.ts files - Add typesOutputDir option for custom output directory - Generate interface based on CSV schema definitions - Export RowType for explicit type imports Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
+28
-5
@@ -41,6 +41,8 @@ module.exports = {
|
||||
bom: true, // 处理 BOM (默认 true)
|
||||
comment: '#', // 忽略 # 开头的注释行 (默认 '#')
|
||||
trim: true, // 修剪表头和值的前后空格 (默认 true)
|
||||
// emitTypes: false, // 禁用类型定义生成 (默认 true)
|
||||
// typesOutputDir: 'types', // 类型文件输出目录 (可选)
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -49,16 +51,35 @@ module.exports = {
|
||||
};
|
||||
```
|
||||
|
||||
### Generated TypeScript Types
|
||||
|
||||
当 `emitTypes: true` 时,loader 会自动生成 `.d.ts` 类型定义文件:
|
||||
|
||||
```typescript
|
||||
// data.schema.d.ts
|
||||
export interface data_schema {
|
||||
name: string;
|
||||
age: number;
|
||||
active: boolean;
|
||||
scores: number[];
|
||||
}
|
||||
|
||||
export type RowType = data_schema;
|
||||
|
||||
declare const data: data_schema[];
|
||||
export default data;
|
||||
```
|
||||
|
||||
### Importing in TypeScript
|
||||
|
||||
```typescript
|
||||
import data from './data.schema.csv';
|
||||
|
||||
// data = [
|
||||
// { name: "Alice", age: 30, active: true, scores: [90, 85, 95] },
|
||||
// { name: "Bob", age: 25, active: false, scores: [75, 80, 70] }
|
||||
// ]
|
||||
```
|
||||
// TypeScript 会自动推断类型:
|
||||
// data: { name: string; age: number; active: boolean; scores: number[] }[]
|
||||
|
||||
// 如果启用了 emitTypes,也可以显式导入类型:
|
||||
// import type { RowType } from './data.schema.csv';
|
||||
|
||||
## Options
|
||||
|
||||
@@ -70,6 +91,8 @@ import data from './data.schema.csv';
|
||||
| `bom` | boolean | `true` | Handle byte order mark |
|
||||
| `comment` | string \| `false` | `#` | Comment character (set `false` to disable) |
|
||||
| `trim` | boolean | `true` | Trim headers and values |
|
||||
| `emitTypes` | boolean | `true` | Generate TypeScript declaration file (.d.ts) |
|
||||
| `typesOutputDir` | string | `''` | Output directory for generated type files (relative to output path) |
|
||||
|
||||
## Schema Syntax
|
||||
|
||||
|
||||
Reference in New Issue
Block a user