fix: use declare module syntax for type definitions

- Change from standalone .d.ts to module augmentation style
- Now properly declares types for the CSV module path
- Fixes TypeScript module resolution

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
hyper 2026-03-31 15:25:58 +08:00
parent 69b419256c
commit 452b600487
2 changed files with 26 additions and 20 deletions

View File

@ -57,17 +57,19 @@ module.exports = {
```typescript ```typescript
// data.schema.d.ts // data.schema.d.ts
export interface data_schema { declare module "./data.schema.csv" {
export interface data_schema {
name: string; name: string;
age: number; age: number;
active: boolean; active: boolean;
scores: number[]; scores: number[];
}
export type RowType = data_schema;
const data: data_schema[];
export default data;
} }
export type RowType = data_schema;
declare const data: data_schema[];
export default data;
``` ```
### Importing in TypeScript ### Importing in TypeScript
@ -79,7 +81,8 @@ import data from './data.schema.csv';
// data: { name: string; age: number; active: boolean; scores: number[] }[] // data: { name: string; age: number; active: boolean; scores: number[] }[]
// 如果启用了 emitTypes也可以显式导入类型: // 如果启用了 emitTypes也可以显式导入类型:
// import type { RowType } from './data.schema.csv'; import type { RowType } from './data.schema.csv';
```
## Options ## Options

View File

@ -54,7 +54,8 @@ function schemaToTypeString(schema: Schema): string {
*/ */
function generateTypeDefinition( function generateTypeDefinition(
resourceName: string, resourceName: string,
propertyConfigs: PropertyConfig[] propertyConfigs: PropertyConfig[],
relativePath: string
): string { ): string {
const interfaceName = path.basename(resourceName, path.extname(resourceName)) const interfaceName = path.basename(resourceName, path.extname(resourceName))
.replace(/[^a-zA-Z0-9_$]/g, '_') .replace(/[^a-zA-Z0-9_$]/g, '_')
@ -64,14 +65,16 @@ function generateTypeDefinition(
.map((config) => ` ${config.name}: ${schemaToTypeString(config.schema)};`) .map((config) => ` ${config.name}: ${schemaToTypeString(config.schema)};`)
.join('\n'); .join('\n');
return `export interface ${interfaceName} { return `declare module "${relativePath}" {
export interface ${interfaceName} {
${properties} ${properties}
}
export type RowType = ${interfaceName};
const data: ${interfaceName}[];
export default data;
} }
export type RowType = ${interfaceName};
declare const data: ${interfaceName}[];
export default data;
`; `;
} }
@ -152,10 +155,10 @@ export default function csvLoader(
// Emit type definition file if enabled // Emit type definition file if enabled
if (emitTypes) { if (emitTypes) {
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs); const relativePath = this.resourcePath.replace(this.context, '').replace(/\\/g, '/');
const relativePath = this.resourcePath.replace(this.context, '');
const dtsFileName = relativePath.replace(/\.csv$/, '.d.ts'); const dtsFileName = relativePath.replace(/\.csv$/, '.d.ts');
const outputPath = path.join(typesOutputDir, dtsFileName); const outputPath = path.join(typesOutputDir, dtsFileName);
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs, relativePath);
this.emitFile?.(outputPath, dtsContent); this.emitFile?.(outputPath, dtsContent);
} }