From 452b600487eddc2544e36948cd2d4ed79766280a Mon Sep 17 00:00:00 2001 From: hyper Date: Tue, 31 Mar 2026 15:25:58 +0800 Subject: [PATCH] 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 --- csv-loader.md | 25 ++++++++++++++----------- src/csv-loader/loader.ts | 21 ++++++++++++--------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/csv-loader.md b/csv-loader.md index ab96428..166c55d 100644 --- a/csv-loader.md +++ b/csv-loader.md @@ -57,17 +57,19 @@ module.exports = { ```typescript // data.schema.d.ts -export interface data_schema { - name: string; - age: number; - active: boolean; - scores: number[]; +declare module "./data.schema.csv" { + export interface data_schema { + name: string; + age: number; + active: boolean; + 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 @@ -79,7 +81,8 @@ import data from './data.schema.csv'; // data: { name: string; age: number; active: boolean; scores: number[] }[] // 如果启用了 emitTypes,也可以显式导入类型: -// import type { RowType } from './data.schema.csv'; +import type { RowType } from './data.schema.csv'; +``` ## Options diff --git a/src/csv-loader/loader.ts b/src/csv-loader/loader.ts index 1f871c6..756c54b 100644 --- a/src/csv-loader/loader.ts +++ b/src/csv-loader/loader.ts @@ -54,7 +54,8 @@ function schemaToTypeString(schema: Schema): string { */ function generateTypeDefinition( resourceName: string, - propertyConfigs: PropertyConfig[] + propertyConfigs: PropertyConfig[], + relativePath: string ): string { const interfaceName = path.basename(resourceName, path.extname(resourceName)) .replace(/[^a-zA-Z0-9_$]/g, '_') @@ -64,14 +65,16 @@ function generateTypeDefinition( .map((config) => ` ${config.name}: ${schemaToTypeString(config.schema)};`) .join('\n'); - return `export interface ${interfaceName} { + return `declare module "${relativePath}" { + export interface ${interfaceName} { ${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 if (emitTypes) { - const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs); - const relativePath = this.resourcePath.replace(this.context, ''); + const relativePath = this.resourcePath.replace(this.context, '').replace(/\\/g, '/'); const dtsFileName = relativePath.replace(/\.csv$/, '.d.ts'); const outputPath = path.join(typesOutputDir, dtsFileName); + const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs, relativePath); this.emitFile?.(outputPath, dtsContent); }