Skip to content

Commit e97a23e

Browse files
committed
generic for Flag config
1 parent def3fe8 commit e97a23e

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

packages/web/src/provider/in-memory-provider/flag-configuration.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ type Variants<T> = Record<string, T>;
1010
/**
1111
* A Feature Flag definition, containing it's specification
1212
*/
13-
export type Flag = {
13+
export type Flag<V = Variants<boolean> | Variants<string> | Variants<number> | Variants<JsonValue>> = {
1414
/**
1515
* An object containing all possible flags mappings (variant -> flag value)
1616
*/
17-
variants: Variants<boolean> | Variants<string> | Variants<number> | Variants<JsonValue>;
17+
variants: V;
1818
/**
1919
* The variant it will resolve to in STATIC evaluation
2020
*/
21-
defaultVariant: string;
21+
defaultVariant: keyof V;
2222
/**
2323
* Determines if flag evaluation is enabled or not for this flag.
2424
* If false, falls back to the default value provided to the client
@@ -30,7 +30,26 @@ export type Flag = {
3030
* If it does not return a valid variant it falls back to the default value provided to the client
3131
* @param EvaluationContext
3232
*/
33-
contextEvaluator?: (ctx: EvaluationContext) => string;
33+
contextEvaluator?: (ctx: EvaluationContext) => keyof V;
34+
};
35+
36+
// sample
37+
const flag: Flag<{
38+
hi: boolean,
39+
bye: boolean,
40+
}> = {
41+
variants: {
42+
'hi': true,
43+
bye: false,
44+
},
45+
disabled: false,
46+
defaultVariant: 'hi',
47+
contextEvaluator: (ctx: EvaluationContext) => {
48+
if (ctx.user === '[email protected]') {
49+
return 'bye';
50+
}
51+
return 'hi';
52+
},
3453
};
3554

3655
export type FlagConfiguration = Record<string, Flag>;

0 commit comments

Comments
 (0)