Skip to main content

Type Generation

Efesto generates TypeScript types from your swaggerModel and keeps them in sync as you edit, so you don't hand-write request/response interfaces.

Express stack only

Type generation (and the magic code editing below) is a feature of the Express stack, where types are derived from swaggerModel / _<verb>Swagger. It runs in development only (isProduction: false). On the Bun/Elysia stack there is nothing to generate: with native Elysia routes TypeScript infers handler types directly from your TypeBox schemas.

How it Works

When you define your swaggerModel and schemas in your service classes, Efesto parses these definitions and generates corresponding TypeScript types.

The generated namespace is named after the route file's default-exported class. A file ending in export default UserService; produces a global namespace UserServiceTypes in <generatedTypesFolder>/UserService.d.ts.

Generated Types

Efesto generates the following types for each endpoint:

  • Request Body Types: Types for the body of POST, PUT, PATCH requests.
  • Response Body Types: Types for the response body.
  • Query Parameters: Types for query string parameters.
  • Path Parameters: Types for URL path parameters.
  • Service Types: Namespace containing all types for a specific service.

Configuration

Set generatedTypesFolder to turn it on:

efesto({
// ...
options: {
generatedTypesFolder: "./src/types", // Folder where types will be generated
automaticTypesGenerationInlineFile: false, // Whether to generate types inline (default: false)
},
});

generatedTypesFolder

This is the directory where Efesto will output the generated .d.ts files. Each service will have its own declaration file in this folder.

automaticTypesGenerationInlineFile

If set to true, Efesto will attempt to generate types directly within the service file. However, the recommended approach is to use generatedTypesFolder to keep types separate.

Magic Code Editing

One of Efesto's most unique features is its ability to "magically" edit your code to add type annotations. When canMagicallyEditCode is enabled in your configuration (default: true), Efesto will:

  1. Analyze your Service Class: It looks at your _get, _post, etc., methods.
  2. Generate Types: It generates the specific Request and Response types based on your Swagger definition.
  3. Update Method Signatures: It automatically updates your method signatures to use these generated types.

Example

Before:

async _get(req, res) {
// ...
}

After (Automatically updated by Efesto):

async _get(req: UserTypes.GetRequest, res: UserTypes.GetResponse) {
// ...
}

This feature significantly speeds up development by providing immediate intellisense and type checking without manual boilerplate.

[!NOTE] This feature only works in development mode. Ensure isProduction is false.

Typings Editor

The typingsEditor tool runs in the background when Efesto starts. It monitors your service files and updates the type definitions whenever you modify your Swagger models.

Generated Namespace

For a service named User, Efesto generates a namespace UserTypes containing:

  • GetRequest, GetResponse
  • PostRequest, PostResponse
  • PutRequest, PutResponse
  • ... and so on for each method.

These types are globally available if you include the generated folder in your tsconfig.json.

tsconfig

Include the generated folder so the types resolve:

{
"include": ["src/**/*", "src/types/**/*"]
}

The types track your swaggerModel while the dev server runs. Commit the folder for reproducible builds, or add it to .gitignore to regenerate on the fly.