Skip to Content
Octopus Energy Packages
CLI

oe.flags generate

Generate typed flags/next declarations from Vercel Flags or Global Config

The oe.flags generate command reads flag definitions from one or more sources and writes a TypeScript declaration file that augments flags/next with typed overloads.

Usage

Generate from Vercel Flags:

npm exec oe.flags generate --vercel --out generated/flags.d.ts

Generate from a Global Config item:

npm exec oe.flags generate --global-config flags --out generated/flags.d.ts

Generate from both sources:

npm exec oe.flags generate --vercel --global-config flags --out generated/flags.d.ts

Options

OptionDescription
--vercelRead active flags from Vercel Flags
--global-config <item-key>Read flags from a Global Config item (repeatable)
-o, --out <path>Declaration file to write (required)

At least one source (--vercel or --global-config) is required.

The Vercel source resolves the local vercel package from the app first, then falls back to vercel on PATH.

Environment loading

Global Config sources need a connection string in GLOBAL_CONFIG or EDGE_CONFIG. GLOBAL_CONFIG takes precedence when both are set. The CLI provides global flags to control how .env files are loaded before commands run:

Global flagBehaviour
--varlockLoad env via varlock
--next-envLoad env via @next/env (reads .env* files)
(default)Auto-detect: tries varlock first, falls back to @next/env
npm exec oe.flags --next-env generate --global-config flags --out generated/flags.d.ts

Output

The generated file augments flags/next with a typed flag overload:

// This file is generated by `oe.flags generate`.
// Do not edit this file manually.

import type { GeneratedFlag, GeneratedFlagDeclaration } from "@octopus-energy/flags";

type GeneratedFlagValues = {
  "boolean-flag": boolean;
  "json-flag":
    { readonly bar: { readonly baz: "qux" } } | { readonly foo: { readonly bar: "baz" } };
  "number-flag": 0 | 1 | 2;
  "string-flag": "foo" | "bar";
};

declare module "flags/next" {
  export function flag<Key extends Extract<keyof GeneratedFlagValues, string>, EntitiesType = any>(
    definition: GeneratedFlagDeclaration<GeneratedFlagValues, Key, EntitiesType>,
  ): GeneratedFlag<GeneratedFlagValues, Key, EntitiesType>;
}

This gives keyed inference when declaring flags:

import { flag } from "flags/next";

export const stringFlag = flag({
  key: "string-flag",
  adapter: myAdapter,
});

const value = await stringFlag();
//    ^? "foo" | "bar"

Module augmentation behavior

Module augmentation adds an overload to the Flags SDK. It improves inference for generated keys but does not remove the SDK's original broad overload for arbitrary keys.

Global Config format

Global Config generation reads an item containing flag definitions. The default item key used by examples is flags:

{
  "checkout-redesign": {
    "kind": "boolean",
    "defaultVariant": "off",
    "variants": {
      "on": { "value": true, "label": "Enabled" },
      "off": { "value": false, "label": "Disabled" }
    }
  },
  "headline-variant": {
    "kind": "string",
    "defaultVariant": "control",
    "variants": {
      "control": { "value": "control", "label": "Control" },
      "short": { "value": "short", "label": "Short headline" },
      "long": { "value": "long", "label": "Long headline" }
    }
  }
}

On this page