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:
parent
69b419256c
commit
452b600487
|
|
@ -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" {
|
||||||
name: string;
|
export interface data_schema {
|
||||||
age: number;
|
name: string;
|
||||||
active: boolean;
|
age: number;
|
||||||
scores: 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
|
### 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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue