Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51fffb7

Browse files
pavkamroncohen
authored andcommitted
fix(react-sdk): improve type definitions for useFeature hook (#324)
Refactor type handling for useFeature to better support different feature configurations and improve type safety
1 parent 00fb33f commit 51fffb7

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

packages/react-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bucketco/react-sdk",
3-
"version": "3.0.0-alpha.5",
3+
"version": "3.0.0-alpha.6",
44
"license": "MIT",
55
"repository": {
66
"type": "git",

packages/react-sdk/src/index.tsx

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ type MaterializedFeatures = keyof Features extends never
3030
: Features;
3131

3232
export type FeatureKey = keyof MaterializedFeatures;
33-
export type FeatureConfig<TKey extends FeatureKey> =
34-
MaterializedFeatures[TKey] extends boolean
35-
? never
36-
: MaterializedFeatures[TKey];
3733

3834
type ProviderContextType = {
3935
client?: BucketClient;
@@ -167,18 +163,22 @@ type RequestFeedbackOptions = Omit<
167163
"featureKey" | "featureId"
168164
>;
169165

166+
type EmptyConfig = {
167+
key: undefined;
168+
payload: undefined;
169+
};
170+
170171
type Feature<TKey extends FeatureKey> = {
171172
isEnabled: boolean;
172173
isLoading: boolean;
173-
config:
174-
| {
175-
key: string;
176-
payload: FeatureConfig<TKey>;
177-
}
178-
| {
179-
key: undefined;
180-
payload: undefined;
181-
};
174+
config: MaterializedFeatures[TKey] extends boolean
175+
? EmptyConfig
176+
:
177+
| {
178+
key: string;
179+
payload: MaterializedFeatures[TKey];
180+
}
181+
| EmptyConfig;
182182
track: () => void;
183183
requestFeedback: (opts: RequestFeedbackOptions) => void;
184184
};
@@ -194,7 +194,9 @@ type Feature<TKey extends FeatureKey> = {
194194
* }
195195
* ```
196196
*/
197-
export function useFeature<TKey extends FeatureKey>(key: TKey): Feature<TKey> {
197+
export function useFeature<TKey extends FeatureKey>(
198+
key: TKey,
199+
): Feature<typeof key> {
198200
const {
199201
features: { isLoading },
200202
client,
@@ -226,7 +228,7 @@ export function useFeature<TKey extends FeatureKey>(key: TKey): Feature<TKey> {
226228
return feature.isEnabled ?? false;
227229
},
228230
get config() {
229-
return feature.config ?? { key: undefined, payload: undefined };
231+
return feature.config as Feature<typeof key>["config"];
230232
},
231233
};
232234
}

0 commit comments

Comments
 (0)