Skip to content

Commit 88c7f57

Browse files
committed
Merge remote-tracking branch 'prettywood/type-safe-flagsmith' into chores/add_typescript_generics
# Conflicts: # flagsmith/types.d.ts
2 parents 935a0ce + 5282ffc commit 88c7f57

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

flagsmith/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { IFlagsmith } from './types';
22
declare const flagsmith: IFlagsmith;
33
export default flagsmith;
4-
export declare const createFlagsmithInstance: () => IFlagsmith;
4+
export declare const createFlagsmithInstance: <
5+
F extends string = string,
6+
T extends string = string,
7+
>() => IFlagsmith<F, T>;

flagsmith/react/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React, { FC } from 'react';
22
import { IFlagsmith, IFlagsmithTrait, IFlagsmithFeature, IState } from '../types';
33
export declare const FlagsmithContext: React.Context<IFlagsmith>;
4-
export declare type FlagsmithContextType = {
5-
flagsmith: IFlagsmith;
6-
options?: Parameters<IFlagsmith['init']>[0];
7-
serverState?: IState;
4+
export declare type FlagsmithContextType<F extends string = string, T extends string = string> = {
5+
flagsmith: IFlagsmith<F, T>;
6+
options?: Parameters<IFlagsmith<F, T>['init']>[0];
7+
serverState?: IState<F, T>;
88
children: React.ReactElement[] | React.ReactElement;
99
};
1010
export declare const FlagsmithProvider: FC<FlagsmithContextType>;

flagsmith/types.d.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,33 @@ export interface IFlagsmithFeature {
33
value?: string | number | boolean;
44
}
55
export declare type IFlagsmithTrait = string | number | boolean;
6-
export interface IFlags {
7-
[key: string]: IFlagsmithFeature;
8-
}
9-
export interface ITraits {
10-
[key: string]: IFlagsmithTrait;
11-
}
6+
7+
export type IFlags<F extends string = string> = Record<F, IFlagsmithFeature>;
8+
9+
export type ITraits<T extends string = string> = Record<T, IFlagsmithTrait>;
10+
1211
export interface IRetrieveInfo {
1312
isFromServer: boolean;
1413
flagsChanged: boolean;
1514
traitsChanged: boolean;
1615
}
17-
export interface IState {
16+
export interface IState<F extends string = string, T extends string = string> {
1817
api: string;
1918
environmentID: string;
20-
flags?: IFlags;
19+
flags?: IFlags<F>;
2120
identity?: string;
22-
traits: ITraits;
21+
traits: ITraits<T>;
2322
}
2423
declare type ICacheOptions = {
2524
ttl?: number;
2625
skipAPI?: boolean;
2726
};
28-
export interface IInitConfig {
27+
export interface IInitConfig<F extends string = string, T extends string = string> {
2928
AsyncStorage?: any;
3029
api?: string;
3130
cacheFlags?: boolean;
3231
cacheOptions?: ICacheOptions;
33-
defaultFlags?: IFlags;
32+
defaultFlags?: Partial<IFlags<F>>;
3433
fetch?: any;
3534
enableAnalytics?: boolean;
3635
enableDynatrace?: boolean;
@@ -39,28 +38,28 @@ export interface IInitConfig {
3938
environmentID: string;
4039
headers?: object;
4140
identity?: string;
42-
traits?: ITraits;
43-
onChange?: (previousFlags: IFlags, params: IRetrieveInfo) => void;
41+
traits?: ITraits<T>;
42+
onChange?: (previousFlags: IFlags<F>, params: IRetrieveInfo) => void;
4443
onError?: (res: {
4544
message: string;
4645
}) => void;
4746
preventFetch?: boolean;
4847
state?: IState;
4948
_trigger?: () => void;
5049
}
51-
export interface IFlagsmith {
50+
export interface IFlagsmith<F extends string = string, T extends string = string> {
5251
/**
5352
* Initialise the sdk against a particular environment
5453
*/
55-
init: (config: IInitConfig) => Promise<void>;
54+
init: (config: IInitConfig<F, T>) => Promise<void>;
5655
/**
5756
* Trigger a manual fetch of the environment features
5857
*/
5958
getFlags: () => Promise<null>;
6059
/**
6160
* Returns the current flags
6261
*/
63-
getAllFlags: () => IFlags;
62+
getAllFlags: () => IFlags<F>;
6463
/**
6564
* Identify user, triggers a call to get flags if flagsmith.init has been called
6665
*/
@@ -88,23 +87,23 @@ export interface IFlagsmith {
8887
/**
8988
* Get the whether a flag is enabled e.g. flagsmith.hasFeature("powerUserFeature")
9089
*/
91-
hasFeature: (key: string) => boolean;
90+
hasFeature: (key: F) => boolean;
9291
/**
9392
* Get the value of a particular remote config e.g. flagsmith.getValue("font_size")
9493
*/
95-
getValue: (key: string) => string | number | boolean;
94+
getValue: (key: F) => string | number | boolean;
9695
/**
9796
* Get the value of a particular trait for the identified user
9897
*/
99-
getTrait: (key: string) => string | number | boolean;
98+
getTrait: (key: T) => string | number | boolean;
10099
/**
101100
* Set a specific trait for a given user id, triggers a call to get flags
102101
*/
103-
setTrait: (key: string, value: string | number | boolean) => Promise<null>;
102+
setTrait: (key: T, value: string | number | boolean) => Promise<null>;
104103
/**
105104
* Set a key value set of traits for a given user, triggers a call to get flags
106105
*/
107-
setTraits: (traits: Record<string, string | number | boolean>) => Promise<null>;
106+
setTraits: (traits: Record<T, string | number | boolean>) => Promise<null>;
108107
/**
109108
* The stored identity of the user
110109
*/

0 commit comments

Comments
 (0)