flagsmith-js-client icon indicating copy to clipboard operation
flagsmith-js-client copied to clipboard

feat: Add interface types

Open kyle-ssg opened this issue 11 months ago • 1 comments

  • Allows providing a full type definition of existing flags and typed remote configuration for: flagsmith, useFlags, useFlagsmith, hasFeature and getValue
  • Supports type file generated by https://github.com/Flagsmith/flagsmith-cli/pull/24
  • In order for this to be fully supported, useFlags will now attempt to silently parse JSON types
export interface FlagsmithTypes {
    "4eyes":                              null;
    a_temp_feature:                       number;
    allow_client_traits:                  null;
    announcement:                         Announcement;
}
export type export interface Announcement {
    id?:          string;
    title?:       string;
    description?: string;
    buttonText?:  string;
    url?:         string;
    isClosable?:  boolean;
}

Example 1

import flagsmith as IFlagsmith<FlagsmithTypes> // or const flagsmith = useFlagsmith<FlagsmithTypes>()
flagsmith.getValue("announcement",{json:true}) // type: Announcement
flagsmith.getValue("fail") // fails types
flagsmith.hasFeature("announcement") // passes
flagsmith.hasFeature("fail") // fails types

Example 2

const {announcement} = useFlags<FlagsmithTypes>(["announcement"]) // announcement.value is of type Announcement
const {fail} = useFlags<FlagsmithTypes>(["fail"]) // fails types

kyle-ssg avatar Mar 19 '25 16:03 kyle-ssg

According to Semantic versioning features should be a minor version bump @kyle-ssg.

jhoermann avatar Mar 20 '25 08:03 jhoermann