Type safety

Reflag offers type safety which reduces errors and frustation

Type safety in Reflag ensures that flag key typos become build errors. This guarantees that if you try to use a non-existent flag key, you'll receive a type error, preventing potential runtime errors and improving code reliability. For remote config, it also ensures that the shape of the payload defined on Reflag matches what your code expects.

Use the Reflag CLI to generate flag types from their definition in Reflag. Once flag types have been generated they are automatically picked up by TypeScript.

It's recommended that you do not check in the flag types, but instead generate them in your build process and on your local development machines.

Set up type safety for flags

  1. Install the Reflag CLI and set up your repository

    npm install --save-dev @reflag/cli
    npx reflag init
  2. Generate the types locally

    npx reflag types generate
  3. Add the generated files to .gitignore

    echo "gen/flags.d.ts" >> .gitignore
  4. Retrieve an API Key for your build system

  5. Set up your build system to run the Reflag CLI to generate types (use the --api-key option or specify the API key in the REFLAG_API_KEY environment variable)

    npx reflag apps list --api-key $REFLAG_API_KEY

Example CI workflow:

# GitHub Actions example
- name: Generate types
  run: npx reflag flags types --api-key ${{ secrets.REFLAG_API_KEY }}

# GitHub Actions example (using environment):
- name: Generate types (environment)
  run: npx reflag flags types
  env:
    REFLAG_API_KEY: ${{ secrets.REFLAG_CI_API_KEY }}

There's further guidance and examples of using Reflag CLI in CI/CD pipelines.

Last updated

Was this helpful?