Skip to content

TypeScript: Add support for export type syntax for interfaces (isolatedModules compatibility) #2429

@tuler

Description

@tuler

Why do we need this improvement?

When generating TypeScript models with tsModelType: interface, the generator produces:

     interface ErrorPayload {                                                                                                                                                                                      
       type: "error";                                                                                                                                                                                              
       message: string;                                                                                                                                                                                            
     }                                                                                                                                                                                                             
     export { ErrorPayload };                                                                                                                                                                                      

This syntax is incompatible with TypeScript's isolatedModules: true compiler option, which is required by modern frameworks like Next.js. With isolatedModules enabled, TypeScript requires type-only exports to use the export type syntax:

     interface ErrorPayload {                                                                                                                                                                                      
       type: "error";                                                                                                                                                                                              
       message: string;                                                                                                                                                                                            
     }                                                                                                                                                                                                             
     export type { ErrorPayload };  // Required for isolatedModules                                                                                                                                                

Error Message

error TS1205: Re-exporting a type when 'isolatedModules' is enabled requires using 'export type'.

How will this change help?

 Use Case                                                                                                                                                                                                      
                                                                                                                                                                                                               
 - Next.js projects (requires isolatedModules: true)                                                                                                                                                           
 - Any TypeScript project using isolatedModules for faster builds                                                                                                                                              
 - Projects using SWC or esbuild which require isolatedModules-compatible code                                                                                                                                 

Screenshots

No response

How could it be implemented/designed?

Add a new TypeScript-specific option (e.g., --tsExportTypeOnly or extend --tsExportType with a type value) that generates export type { } for interfaces while keeping export { } for enums (which are
runtime values).

 For interfaces:                                                                                                                                                                                               
 export type { ErrorPayload };                                                                                                                                                                                 
                                                                                                                                                                                                               
 For enums (unchanged):                                                                                                                                                                                        
 export { ErrorCode };                                                                                                                                                                                         
                                                                                                                                                                                                               
 Affected Code                                                                                                                                                                                                 
                                                                                                                                                                                                               
 The export logic is in TypeScriptDependencyManager.renderExport():                                                                                                                                            
     // Current implementation (ESM named exports):                                                                                                                                                                
     return `export { ${model.name} };`;                                                                                                                                                                           
                                                                                                                                                                                                                   
     // Proposed change when type-only export is enabled:                                                                                                                                                          
     if (options.typeOnlyExport && model.type === 'interface') {                                                                                                                                                   
       return `export type { ${model.name} };`;                                                                                                                                                                    
     }                                                                                                                                                                                                             
     return `export { ${model.name} };`;                                                                                                                                                                           
 Environment                                                                                                                                                                                                   
                                                                                                                                                                                                               
 - Modelina version: latest (via @asyncapi/cli v6.0.0)                                                                                                                                                         
 - TypeScript version: 5.x                                                                                                                                                                                     
 - AsyncAPI CLI command:                                                                                                                                                                                       
 asyncapi generate models typescript spec.yaml --tsModelType interface --tsEnumType enum --tsExportType named                                                                                                  
                                                                                                                                                                                                               
 Workaround                                                                                                                                                                                                    
                                                                                                                                                                                                               
 Currently requires post-processing generated files to convert export { } to export type { } for interfaces, which is error-prone and adds build complexity.

🚧 Breaking changes

Yes

👀 Have you checked for similar open issues?

  • I checked and didn't find a similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions