8000 feat: support type-safe flag keys via module augmentation by MattIPv4 · Pull Request #1349 · open-feature/js-sdk · GitHub
[go: up one dir, main page]

Skip to content

feat: support type-safe flag keys via module augmentation#1349

Open
MattIPv4 wants to merge 3 commits intoopen-feature:mainfrom
MattIPv4:MattIPv4/type-safe-flag-keys
Open

feat: support type-safe flag keys via module augmentation#1349
MattIPv4 wants to merge 3 commits intoopen-feature:mainfrom
MattIPv4:MattIPv4/type-safe-flag-keys

Conversation

@MattIPv4
Copy link
Member
@MattIPv4 MattIPv4 commented Feb 20, 2026

This PR

Adds support for consumers defining restricted types for flag keys when calling methods, via TypeScript module augmentation. Providing an alternative to using the CLI to code-gen type-safe wrapper hooks/methods.

Related Issues

Resolves #1341

Notes

My only concern would be whether ConstrainedFlagKey is going to behave correctly for the generic methods like useFlag.

image image image

Follow-up Tasks

N/A

How to test

I'm open to suggestions on how to encode tests for this in the repository itself. The notes in the READMEs shoud explain how one would test/use this as a consumer.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @MattIPv4, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the developer experience by integrating type-safe flag keys into the OpenFeature SDKs. By allowing module augmentation, it provides a flexible and powerful mechanism for developers to enforce strict typing on flag keys, leading to fewer runtime errors and better code maintainability. This change streamlines the process of working with feature flags by offering enhanced autocompletion and compile-time validation, making it easier to manage and evaluate flags correctly across different application environments.

Highlights

  • Type-Safe Flag Keys: Introduced support for type-safe flag keys across Angular, Nest, React, Server, and Web SDKs using TypeScript module augmentation. This allows consumers to define restricted types for flag keys, enhancing type safety and autocompletion without requiring CLI code generation.
  • Documentation Updates: Added comprehensive documentation in the README files for Angular, Nest, React, Server, and Shared SDKs, explaining how to implement and benefit from type-safe flag keys via module augmentation.
  • API Type Enhancements: Updated various flag evaluation methods and directive inputs to leverage the new BooleanFlagKey, StringFlagKey, NumberFlagKey, ObjectFlagKey, and ConstrainedFlagKey types, ensuring type enforcement at compile time.
Changelog
  • packages/angular/projects/angular-sdk/README.md
    • Added documentation section for type-safe flag keys.
  • packages/angular/projects/angular-sdk/src/lib/feature-flag.directive.ts
    • Imported specific flag key types from '@openfeature/web-sdk'.
    • Updated booleanFeatureFlag input type to BooleanFlagKey.
    • Updated numberFeatureFlag input type to NumberFlagKey.
    • Updated stringFeatureFlag input type to StringFlagKey.
    • Updated objectFeatureFlag input type to ObjectFlagKey.
  • packages/angular/projects/angular-sdk/src/lib/feature-flag.service.ts
    • Imported specific flag key types from '@openfeature/web-sdk'.
    • Updated flagKey parameter type in getBooleanDetails to BooleanFlagKey.
    • Updated flagKey parameter type in getStringDetails to StringFlagKey.
    • Updated flagKey parameter type in getNumberDetails to NumberFlagKey.
    • Updated flagKey parameter type in getObjectDetails to ObjectFlagKey.
  • packages/nest/README.md
    • Added documentation section for type-safe flag keys.
  • packages/nest/src/feature.decorator.ts
    • Imported ConstrainedFlagKey type from '@openfeature/server-sdk'.
    • Updated flagKey property type in FeatureProps interface to ConstrainedFlagKey<T>.
  • packages/nest/src/require-flags-enabled.decorator.ts
    • Imported BooleanFlagKey type from '@openfeature/server-sdk'.
    • Updated flagKey property type in RequiredFlag interface to BooleanFlagKey.
  • packages/react/README.md
    • Added documentation section for type-safe flag keys.
  • packages/react/src/declarative/FeatureFlag.tsx
    • Imported ConstrainedFlagKey type from '@openfeature/web-sdk'.
    • Updated flagKey property type in FeatureFlagProps interface to ConstrainedFlagKey<T>.
  • packages/react/src/evaluation/use-feature-flag.ts
    • Imported specific flag key types and ConstrainedFlagKey from '@openfeature/web-sdk'.
    • Updated flagKey parameter type in useFlag to ConstrainedFlagKey<T>.
    • Updated flagKey parameter type in useSuspenseFlag to ConstrainedFlagKey<T>.
    • Updated flagKey parameter type in useBooleanFlagValue and useBooleanFlagDetails to BooleanFlagKey.
    • Updated flagKey parameter type in useStringFlagValue and useStringFlagDetails to StringFlagKey.
    • Updated flagKey parameter type in useNumberFlagValue and useNumberFlagDetails to NumberFlagKey.
    • Updated flagKey parameter type in useObjectFlagValue and useObjectFlagDetails to ObjectFlagKey.
  • packages/server/README.md
    • Added documentation section for type-safe flag keys.
  • packages/server/src/evaluation/evaluation.ts
    • Imported specific flag key types from '@openfeature/core'.
    • Updated flagKey parameter types in Features interface methods (getBooleanValue, getBooleanDetails, getStringValue, getStringDetails, getNumberValue, getNumberDetails, getObjectValue, getObjectDetails) to their respective type-safe flag key types (e.g., BooleanFlagKey, StringFlagKey).
  • packages/shared/README.md
    • Added a new section 'Type-Safe Flag Keys' with detailed explanation and examples for module augmentation.
  • packages/shared/src/evaluation/evaluation.ts
    • Introduced new type FlagKey as a base for all flag keys.
    • Defined BooleanFlagKey, StringFlagKey, NumberFlagKey, and ObjectFlagKey types, inheriting from FlagKey by default.
    • Added ConstrainedFlagKey type for conditional type inference based on flag value type.
  • packages/web/README.md
    • Added documentation section for type-safe flag keys.
  • packages/web/src/evaluation/evaluation.ts
    • Imported specific flag key types from '@openfeature/core'.
    • Updated flagKey parameter types in Features interface methods (getBooleanValue, getBooleanDetails, getStringValue, getStringDetails, getNumberValue, getNumberDetails, getObjectValue, getObjectDetails) to their respective type-safe flag key types (e.g., BooleanFlagKey, StringFlagKey).
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@MattIPv4 MattIPv4 force-pushed the MattIPv4/type-safe-flag-keys branch from ca1ad14 to ee798ab Compare February 20, 2026 16:33
Copy link
Contributor
@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces comprehensive support for type-safe flag keys across the Angular, Nest, React, and Server SDKs, leveraging TypeScript module augmentation. The changes are well-implemented, consistently applying the new *FlagKey types and ConstrainedFlagKey where appropriate. The accompanying documentation updates in the README files clearly explain how consumers can utilize this new feature for enhanced type safety and autocompletion. This is a valuable improvement that will reduce common errors and improve developer experience.

@MattIPv4 MattIPv4 marked this pull request as ready for review February 20, 2026 17:04
@MattIPv4 MattIPv4 requested review from a team as code owners February 20, 2026 17:04
@MattIPv4 MattIPv4 force-pushed the MattIPv4/type-safe-flag-keys branch from ee798ab to 7fcdfaf Compare February 20, 2026 17:21
@beeme1mr
Copy link
Member

Wow, this is really neat! I'll try and test it locally but this seems like a solid approach. We may even be able to extend the OpenFeature CLI to optionally generate the declaration file.

@MattIPv4 MattIPv4 force-pushed the MattIPv4/type-safe-flag-keys branch from 7fcdfaf to ff7fcd6 Compare February 20, 2026 18:03
@beeme1mr
Copy link
Member

Hey @MattIPv4, what's the best way to test this locally?

@MattIPv4
Copy link
Member Author
MattIPv4 commented Feb 20, 2026

I ended up building the packages and then installing them via file/link references in one of our actual projects that uses OpenFeature (and then following the README to add the augmentation to the project).

I think you could probably spin up a very minimal TS project that just installs the web SDK w/ some fake getClient().getBooleanValue calls w/ the augmentation?

You can also create a new directory packages/web/test/augmentation and have the augmentation file + a test TS file + a tsconfig in there (and the parent tsconfig marked to exclude the new directory), which will allow you to see how this behaves. However, I didn't commit this as I cannot get tsc to actually pass cleanly on that, as it turns out you seemingly can't augment a module when it is mapped via compilerOptions.paths.

@toddbaert
Copy link
Member

This is rad

Copy link
Member
@lukas-reining lukas-reining left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MattIPv4!
This looks really cool!
I will do a proper test and review end of the weekend or beginning of next week :)

Signed-off-by: MattIPv4 <me@mattcowley.co.uk>
Signed-off-by: MattIPv4 <me@mattcowley.co.uk>
Signed-off-by: MattIPv4 <me@mattcowley.co.uk>
@MattIPv4 MattIPv4 force-pushed the MattIPv4/type-safe-flag-keys branch from e81d87b to 222bff7 Compare February 21, 2026 03:18
Copy link
Member
@beeme1mr beeme1mr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed to work perfectly in my tests. I'd like others to try before we merge but I think this is pretty cool and a great addition. Nice job @MattIPv4!

Copy link
Member
@lukas-reining lukas-reining left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it out with all the SDKs and really like this change.
Cool idea I did not have in my mind at all!

Comment on lines +24 to +26
* declare module '@openfeature/core' {
* export type StringFlagKey = 'my-string-flag';
* }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants

0