Skip to content
This repository was archived by the owner on Sep 25, 2025. It is now read-only.

Commit b6f1bc6

Browse files
Merge pull request #79 from sevenwestmedia-labs/feature/improve-client-typings
Feature: Improve client typings
2 parents 7600522 + 95bcd7f commit b6f1bc6

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@etrigan/feature-toggles-client': minor
3+
---
4+
5+
Improve typings via module augmentation.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@
4343
"eslint": "^7.12.1",
4444
"eslint-config-prettier": "^7.0.0",
4545
"eslint-config-wanews-base": "^2.2.0"
46-
}
46+
},
47+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
4748
}

packages/feature-toggles-client/src/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
1-
export interface FeatureValue {
2-
raw: any
1+
export interface FeatureValue<TValue = any> {
2+
raw: TValue
33
string(): string
44
boolean(): boolean
55
}
66

7+
/**
8+
* Augment this interface at a global-level to improve the typing of returned features.
9+
* https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
10+
*/
711
export interface RawFeatureValues {
812
[feature: string]: unknown
913
}
14+
1015
/**
1116
* Current state of the feature toggles
1217
*/
13-
export interface FeatureState {
14-
[feature: string]:
18+
export type FeatureState = {
19+
[key in keyof RawFeatureValues]?:
1520
| {
16-
value: FeatureValue
21+
value: FeatureValue<RawFeatureValues[key]>
1722

1823
/** User overridable */
1924
canUserOverride: boolean
2025

21-
userOverride?: FeatureValue
26+
userOverride?: FeatureValue<RawFeatureValues[key]>
2227
}
2328
| undefined
2429
}
2530

26-
export function isFeatureEnabled<Features extends string>(
31+
export function isFeatureEnabled<TKey extends keyof RawFeatureValues>(
2732
toggles: FeatureState,
28-
feature: Features,
33+
feature: TKey,
2934
fallback = false,
3035
): boolean {
3136
const featureState = toggles[feature]

0 commit comments

Comments
 (0)