Skip to content

Commit 1c49654

Browse files
authored
Merge pull request #315 from Flagsmith/fix/javascript-client-types
fix: updated-interface-flags-for-javascript-client
2 parents 0a7f145 + 309c81a commit 1c49654

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

lib/flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith",
3-
"version": "9.2.0",
3+
"version": "9.2.1",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"module": "./index.mjs",

lib/react-native-flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-flagsmith",
3-
"version": "9.2.0",
3+
"version": "9.2.1",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

test/types.test.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Sample test
22
import {getFlagsmith} from './test-constants';
3-
import {IFlagsmith} from '../types';
3+
import {IFlagsmith, IFlagsmithFeature} from '../types';
44

55
describe('Flagsmith Types', () => {
66

@@ -20,13 +20,45 @@ describe('Flagsmith Types', () => {
2020
typedFlagsmith.getValue("flag2")
2121
});
2222
test('should allow supplying interface generics to a flagsmith instance', async () => {
23-
const { flagsmith, } = getFlagsmith({ });
23+
const { flagsmith } = getFlagsmith({});
2424
const typedFlagsmith = flagsmith as IFlagsmith<
2525
{
2626
stringFlag: string
2727
numberFlag: number
2828
objectFlag: { first_name: string }
2929
}>
30+
typedFlagsmith.init({
31+
environmentID: "test",
32+
defaultFlags: {
33+
stringFlag: {
34+
id: 1,
35+
enabled: true,
36+
value: "string_value"
37+
},
38+
numberFlag: {
39+
id: 2,
40+
enabled: true,
41+
value: 123
42+
},
43+
objectFlag: {
44+
id: 3,
45+
enabled: true,
46+
value: JSON.stringify({ first_name: "John" })
47+
}
48+
},
49+
onChange: (previousFlags) => {
50+
//eslint-disable-next-line @typescript-eslint/no-unused-vars
51+
const previousStringFlag = previousFlags?.stringFlag
52+
//eslint-disable-next-line @typescript-eslint/no-unused-vars
53+
const previousNumberFlag = previousFlags?.numberFlag
54+
//eslint-disable-next-line @typescript-eslint/no-unused-vars
55+
const previousObjectFlag = previousFlags?.objectFlag
56+
//@ts-expect-error - flag does not exist
57+
//eslint-disable-next-line @typescript-eslint/no-unused-vars
58+
const previousNonExistingFlag = previousFlags?.nonExistingFlag
59+
}
60+
})
61+
3062
//@ts-expect-error - feature not defined
3163
typedFlagsmith.hasFeature("fail")
3264
//@ts-expect-error - feature not defined
@@ -36,6 +68,18 @@ describe('Flagsmith Types', () => {
3668
typedFlagsmith.hasFeature("numberFlag")
3769
typedFlagsmith.getValue("stringFlag")
3870
typedFlagsmith.getValue("numberFlag")
71+
72+
const typedFlags = await typedFlagsmith.getAllFlags()
73+
//eslint-disable-next-line @typescript-eslint/no-unused-vars
74+
const asString = typedFlags.stringFlag
75+
//eslint-disable-next-line @typescript-eslint/no-unused-vars
76+
const asNumber = typedFlags.numberFlag
77+
//eslint-disable-next-line @typescript-eslint/no-unused-vars
78+
const asObject = typedFlags.objectFlag
79+
80+
// @ts-expect-error - invalid does not exist on type
81+
//eslint-disable-next-line @typescript-eslint/no-unused-vars
82+
const asNonExisting = typedFlags.nonExistingFlag
3983

4084
//eslint-disable-next-line @typescript-eslint/no-unused-vars
4185
const stringFlag: string | null = typedFlagsmith.getValue("stringFlag")
@@ -44,7 +88,7 @@ describe('Flagsmith Types', () => {
4488
//eslint-disable-next-line @typescript-eslint/no-unused-vars
4589
const firstName: string | undefined = typedFlagsmith.getValue("objectFlag")?.first_name
4690

47-
// @ts-expect-error - invalid does not exist on type announcement
91+
// @ts-expect-error - invalid does not exist on type
4892
//eslint-disable-next-line @typescript-eslint/no-unused-vars
4993
const invalidPointer: string = typedFlagsmith.getValue("objectFlag")?.invalid
5094

types.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface IRetrieveInfo {
4949

5050
export interface IState<F extends string = string> {
5151
api: string;
52-
flags?: IFlags<F>;
52+
flags?: IFlags<FKey<F>>;
5353
evaluationContext?: EvaluationContext;
5454
evaluationEvent?: Record<string, Record<string, number>> | null;
5555
ts?: number;
@@ -89,21 +89,21 @@ export declare type LoadingState = {
8989
source: FlagSource //Indicates freshness of flags
9090
}
9191

92-
export type OnChange<F extends string = string> = (previousFlags: IFlags<F> | null, params: IRetrieveInfo, loadingState:LoadingState) => void
92+
export type OnChange<F extends string = string> = (previousFlags: IFlags<FKey<F>> | null, params: IRetrieveInfo, loadingState:LoadingState) => void
9393

9494
export type ApplicationMetadata = {
9595
name: string;
9696
version?: string;
9797
}
9898

99-
export interface IInitConfig<F extends string = string, T extends string = string> {
99+
export interface IInitConfig<F extends string | Record<string, any> = string, T extends string = string> {
100100
AsyncStorage?: any;
101101
api?: string;
102102
evaluationContext?: ClientEvaluationContext;
103103
cacheFlags?: boolean;
104104
cacheOptions?: ICacheOptions;
105105
datadogRum?: IDatadogRum;
106-
defaultFlags?: IFlags<F>;
106+
defaultFlags?: IFlags<FKey<F>>;
107107
fetch?: any;
108108
realtime?: boolean;
109109
eventSourceUrl?: string;
@@ -168,7 +168,7 @@ T extends string = string
168168
/**
169169
* Initialise the sdk against a particular environment
170170
*/
171-
init: (config: IInitConfig<F, T>) => Promise<void>;
171+
init: (config: IInitConfig<FKey<F>, T>) => Promise<void>;
172172
/**
173173
* Set evaluation context. Refresh the flags.
174174
*/
@@ -189,7 +189,7 @@ T extends string = string
189189
/**
190190
* Returns the current flags
191191
*/
192-
getAllFlags: () => IFlags<F>;
192+
getAllFlags: () => IFlags<FKey<F>>;
193193
/**
194194
* Identify user, triggers a call to get flags if `flagsmith.init` has been called
195195
* */

0 commit comments

Comments
 (0)