Utilities
defineFlags
Section titled “defineFlags”A type-safe helper function for defining feature flags. It validates that all flag values are of valid types (boolean, string, or number) at the type level.
import { defineFlags } from '@localflag/react';
export const defaultFlags = defineFlags({ darkMode: false, newDashboard: true, maxItems: 10, apiVersion: "v1",});
export type AppFlags = typeof defaultFlags;Signature
Section titled “Signature”function defineFlags<T extends Record<string, FlagValue>>(flags: T): TParameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
flags | Record<string, FlagValue> | An object containing flag names and their default values |
Returns
Section titled “Returns”Returns the same flags object unchanged. This function has no runtime overhead—it exists purely for type safety.
Type Constraint
Section titled “Type Constraint”The FlagValue type accepts:
boolean- For on/off flagsstring- For variant flags or configurationnumber- For numeric limits or thresholds
// ValiddefineFlags({ enabled: true, // boolean theme: "dark", // string maxRetries: 3, // number});
// Type error: arrays and objects are not valid flag valuesdefineFlags({ tags: ["a", "b"], // Error: not a valid FlagValue config: { key: 1 }, // Error: not a valid FlagValue});Why Use defineFlags?
Section titled “Why Use defineFlags?”- Type Validation - Catches invalid flag types at compile time
- Consistency - Establishes a clear pattern for flag definitions
- Future-Proof - Enables potential tooling and CLI integrations
Alternative: as const
Section titled “Alternative: as const”If you prefer not to import defineFlags, you can use TypeScript’s as const assertion:
export const defaultFlags = { darkMode: false, newDashboard: true,} as const;
export type AppFlags = typeof defaultFlags;Both approaches provide full type inference. The difference is that defineFlags validates flag value types, while as const allows any value type.
createFlags
Section titled “createFlags”A helper function that defines feature flags with optional descriptions. Returns both the flags object and a descriptions object.
import { createFlags } from '@localflag/react';
export const { flags, descriptions } = createFlags({ darkMode: { value: false, description: "Enable dark theme" }, newDashboard: { value: true, description: "Use new dashboard layout" }, maxItems: 10, // Simple value without description});
export type AppFlags = typeof flags;Signature
Section titled “Signature”function createFlags<T>(config: T): { flags: ExtractFlags<T>; descriptions: Partial<Record<keyof T, string>>;}Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
config | Record<string, FlagValue | FlagConfig> | An object containing flag names with values or { value, description } objects |
Returns
Section titled “Returns”| Property | Type | Description |
|---|---|---|
flags | ExtractFlags<T> | The extracted flag values |
descriptions | Partial<Record<keyof T, string>> | A map of flag names to descriptions |
FlagConfig Type
Section titled “FlagConfig Type”interface FlagConfig<T extends FlagValue = FlagValue> { value: T; description?: string;}Usage with Provider
Section titled “Usage with Provider”Pass both flags and descriptions to the provider:
import { FeatureFlagProvider, LocalFlagDevTools } from '@localflag/react';import { flags, descriptions } from './flags';
function App() { return ( <FeatureFlagProvider defaultFlags={flags} descriptions={descriptions}> <YourApp /> <LocalFlagDevTools /> </FeatureFlagProvider> );}The descriptions will appear in the LocalFlagDevTools panel below each flag name.
Mixed Syntax
Section titled “Mixed Syntax”You can mix simple values and objects with descriptions:
const { flags, descriptions } = createFlags({ // With description darkMode: { value: false, description: "Enable dark theme" },
// Without description (simple value) maxItems: 10, apiVersion: "v1",});