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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user