Env-mage Docs

Typegen

Generate types

typegen

Generates TypeScript type definitions for your environment variables.

Usage

env-mage typegen [options]

Options

OptionDescriptionDefault
--file <path>Source file.env
--output <path>Output fileenv.d.ts
--format <format>Output format (typescript, jsdoc, or both)typescript
--guess-typesTry to infer typestrue
--no-guess-typesTreat all variables as stringsfalse

Example

env-mage typegen --output src/types/env.d.ts --format typescript

Example Output

✓ Reading environment file: .env
✓ Found 8 environment variables
✓ Inferring variable types
  ℹ DATABASE_URL: string
  ℹ PORT: number
  ℹ DEBUG: boolean
  ℹ API_KEY: string
✓ Generating TypeScript types
✓ Writing output to: src/types/env.d.ts
✓ Type definitions created successfully!

Generated File Example

declare namespace NodeJS {
  interface ProcessEnv {
    DATABASE_URL: string;
    PORT: number;
    DEBUG: boolean;
    API_KEY: string;
  }
}

Things to Know

  • Automatically infers types (string, number, boolean) from values
  • Use --no-guess-types for all string types
  • Works with TypeScript projects to provide type safety
  • Import the generated file once in your project for global types