← All posts
Type-Safe Feature Flags with TypeScript
Stringly-typed flags are a footgun. A tiny bit of TypeScript turns typos into compile errors.
Every flag used to be a magic string. Inevitably someone checked "new_checkout" against a flag named "new-checkout" and shipped a no-op.
A typed registry
const FLAGS = {
newCheckout: { default: false },
betaSearch: { default: false },
} as const;
type FlagName = keyof typeof FLAGS;
function useFlag(name: FlagName): boolean {
// name is now autocompleted and typo-proof
return resolve(name) ?? FLAGS[name].default;
}Now useFlag("new_checkout") is a red squiggle, not a production incident. The whole abstraction is fifteen lines and pays for itself the first week.
More to read