Skip to content

[FEATURE] Type-safe flagKey in React SDK (string literal / generic support) #1341

@beautyfree

Description

@beautyfree

Description

Currently, in the OpenFeature React SDK (@openfeature/react-sdk), the flagKey parameter in hooks such as useFlag, useBooleanFlag, useStringFlag, etc. is typed as a plain string.

This results in:

  • no autocomplete for available flag keys,
  • no compile-time validation for typos,
  • potential drift between code and actual feature flag definitions.

It would be very valuable to have a way to make flagKey type-safe, for example via string literal types or generics.


Current behavior

useBooleanFlag('my-feature-flag');

flagKey is typed as string, so TypeScript cannot:

  • suggest valid keys,
  • catch misspellings at compile time.

Desired behavior

Ability to restrict flagKey to a known set of feature flag keys:

type FeatureFlagKey =
  | 'new-dashboard'
  | 'beta-checkout'
  | 'enable-ai';

useBooleanFlag('new-dashbord'); // ❌ TS error
useBooleanFlag('new-dashboard'); // ✅

Or via a generic API:

useBooleanFlag<FeatureFlagKey>('new-dashboard');

Possible approaches

  1. Generic parameter for flagKey

    function useBooleanFlag<K extends string>(
      flagKey: K,
      defaultValue?: boolean
    ): boolean;
  2. Typed provider / context

    const FeatureFlagProvider = createFeatureFlagProvider<FeatureFlagKey>();
  3. Module augmentation
    Allow teams to extend flag key types in their own codebase without forking the SDK.


Why this matters

  • Feature flags are part of business logic; mistakes in flag keys can be critical
  • Type-safe flags:
    • reduce runtime errors,
    • improve developer experience,
    • align well with contract-first approaches
  • Especially valuable in large React codebases

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions