-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[PM-2282] Make feature flags type safe #8612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
No New Or Fixed Issues Found |
// Replace this with a type safe lookup of the feature flag values in PM-2282 | ||
export type FeatureFlagValue = number | string | boolean; | ||
// Map of feature flags to their value type. `string`, `number` and `boolean` are the only supported types. | ||
export const FeatureFlagRuntimeTypes = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we technically don't need these for this PR, #8561 requires it if we want to introduce a default defaultValue
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #8612 +/- ##
==========================================
+ Coverage 27.67% 27.70% +0.02%
==========================================
Files 2369 2369
Lines 69049 69052 +3
Branches 12919 12919
==========================================
+ Hits 19112 19128 +16
+ Misses 48504 48491 -13
Partials 1433 1433 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't really comment on libs/common/src/enums/feature-flag.enum.ts
since it's my own code but added some comments in other parts
return this.serverConfig$.pipe( | ||
map((serverConfig) => { | ||
if (serverConfig?.featureStates == null || serverConfig.featureStates[key] == null) { | ||
return defaultValue; | ||
} | ||
|
||
return serverConfig.featureStates[key] as T; | ||
return serverConfig.featureStates[key] as FeatureFlagType<Flag>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎨 suggestion: Since we now have the types available during runtime, maybe we should add some guards here to verify that we actually get the types we expect?
return serverConfig.featureStates[key] as FeatureFlagType<Flag>; | |
const value = serverConfig.featureStates[key]; | |
if (FeatureFlagRuntimeTypes[key] !== typeof value) { throw new Error("Unknown flag value, possibly undefined?"); } // or maybe return default value? | |
return value as FeatureFlagType<Flag>; |
Or maybe this check should be done when fetching the server config, in which case serverConfig?.featureStates
might already be fully type-safe by this point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was planning on using it in my other PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻 kudos!
🤔 non-blocking concern
⛏️ nitpick
|
||
export type DefaultFeatureFlagValueType = typeof DefaultFeatureFlagValue; | ||
|
||
export type FeatureFlagType<Flag extends FeatureFlag> = DefaultFeatureFlagValueType[Flag]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export type FeatureFlagType<Flag extends FeatureFlag> = DefaultFeatureFlagValueType[Flag]; | |
/** gets the type of a `FeatureFlag` value. */ | |
export type FeatureFlagType<Flag extends FeatureFlag> = DefaultFeatureFlagValueType[Flag]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 Since the default config service has test cases, they should be updated.
Type of change
Objective
Refactors the feature flags in
ConfigService
to be type safe. It also moves the default value to a centralized location rather than the caller defining it. This ensures consistency across the various places they are used.Before you submit