Skip to content

feat: generics for InMemoryProvider Flag config#1046

Closed
wichopy wants to merge 1 commit intoopen-feature:mainfrom
wichopy:wichopy/inMemoryProvider-types
Closed

feat: generics for InMemoryProvider Flag config#1046
wichopy wants to merge 1 commit intoopen-feature:mainfrom
wichopy:wichopy/inMemoryProvider-types

Conversation

@wichopy
Copy link
Copy Markdown
Member

@wichopy wichopy commented Oct 19, 2024

This PR

  • adds this new feature

Related Issues

Fixes #1234523

Notes

Follow-up Tasks

How to test

Copy link
Copy Markdown
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.

Looks good so far!

Comment on lines +36 to 53
// sample
const flag: Flag<{
hi: boolean,
bye: boolean,
}> = {
variants: {
'hi': true,
bye: false,
},
disabled: false,
defaultVariant: 'hi',
contextEvaluator: (ctx: EvaluationContext) => {
if (ctx.user === 'bob@flags.com') {
return 'bye';
}
return 'hi';
},
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this was there for testing?

github-merge-queue Bot pushed a commit that referenced this pull request Apr 10, 2026
## This PR

Applies _a few_ generics to the `Flag` and `FlagConfiguration` types
used by `InMemoryProvider` to allow for type-safe variant values,
ensuring the `defaultVariant` and any value returned by
`contextEvaluator` are present as keys in `variants` for the given flag.

### Related Issues

Resolves #967

Closes #1046

### Notes

If folks are passing in an object reference to `InMemoryProvider`, like
we are in the specs, rather than an object literal directly, this is
_technically_ a breaking change as they'll need to `as const` or pass it
directly, to allow TypeScript to correctly infer the types. I'm
personally of the opinion that this isn't a breaking change in the API
of the package, and so this can ship as a minor change, but open to
being told this should be considered breaking.

Also, the monorepo is using a _very old_ version of TypeScript, 4.4, so
I've had to include a util for `NoInfer` rather than using the built-in
util that's available in TypeScript 5.4 and beyond.

### Follow-up Tasks

N/A

### How to test

See specs where this is already demonstrated to be working as I've had
to apply `@ts-expect-error` when we're using invalid variant keys.

---------

Signed-off-by: MattIPv4 <me@mattcowley.co.uk>
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.

2 participants