You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When defining schemas in AsyncAPI documents, the const keyword is used to enforce that a property must have a specific, fixed value. However, in the current TypeScript model generation, these fixed values are hardcoded in the interfaces, limiting reusability and clarity.
We propose that properties defined with the const keyword in the AsyncAPI schema should also generate exported constants in the TypeScript models. These constants can then be referenced in the generated interfaces, improving code maintainability and consistency.
// Exported constant
export const EVENT_TYPE = "EXAMPLE_EVENT";
// Interface using the constant
export interface ExampleMessage {
eventType: typeof EVENT_TYPE; // Enforces the fixed value
}
How will this change help?
Improved Reusability: The constant can be reused across the codebase instead of hardcoding the value multiple times.
Enhanced Readability: Exported constants make the intent of fixed values more explicit.
Better Maintainability: If the constant value changes, it only needs to be updated in one place.
Alignment with TypeScript Best Practices: Encourages using constants for fixed values instead of inline literals.
Screenshots
No response
How could it be implemented/designed?
Modify the TypeScript template in the AsyncAPI Generator to:
Identify properties with the const keyword.
Generate an exported constant for each property with a const value.
Reference the exported constant in the corresponding interface.
This feature could optionally be configurable (e.g., via a generator flag) to provide flexibility for developers who may not require exported constants.
Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request. Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.
Why do we need this improvement?
When defining schemas in AsyncAPI documents, the const keyword is used to enforce that a property must have a specific, fixed value. However, in the current TypeScript model generation, these fixed values are hardcoded in the interfaces, limiting reusability and clarity.
We propose that properties defined with the const keyword in the AsyncAPI schema should also generate exported constants in the TypeScript models. These constants can then be referenced in the generated interfaces, improving code maintainability and consistency.
Example Input Schema:
Expected Output in TypeScript:
How will this change help?
Screenshots
No response
How could it be implemented/designed?
🚧 Breaking changes
No
👀 Have you checked for similar open issues?
🏢 Have you read the Contributing Guidelines?
Are you willing to work on this issue?
None
The text was updated successfully, but these errors were encountered: