Back

Type-Safe Feature Flags, From Code to LaunchDarkly

Dylan Webb
Senior Software Engineer
7 min read

Feature launch and configuration are a critical part of the product release flow at Clipboard. New experimental features, dynamic copy, region-, version-, or user-specific configurations, and much more can all be controlled on the fly without code changes. At Clipboard, we use LaunchDarkly as our feature flag provider and have had great success with their product over the years. The biggest issue we faced with LaunchDarkly was the lack of type-safety guarantees for the flag values we received. We had no assurance that the type we expected in code matched the actual value. Even if it did initially, one unintentional change could immediately cause production issues because there were no guardrails to maintain typing.

Clipboard manages over 500 feature flags across 10+ repositories, typed at declaration, at runtime, and inside LaunchDarkly itself. Agents can manage the entire lifecycle of a flag with these typed guarantees. In our largest repository, the compiler checks 240 flag schemas, 1,900 Zod nodes nested up to nine levels deep, performing more than 600,000 generic type instantiations in a third of a second without an incremental cache. Here's how we do that.

Declared type safety: the configuration map

The first concern was the TypeScript type check. While we could rely on developers to declare a type and type-cast the output of a flag fetch from the standard any type we receive from LaunchDarkly, this is brittle and relies on every developer and agent remembering to follow this rule at every call site. This made every new flag a potential source of type drift. Instead, we type flag values through a configuration map, which links the flag key to the schema and a known valid default value. This matches each key to the flag's type and schema and ensures that whenever a flag value is fetched from LaunchDarkly, it is typed correctly based on the key used to fetch it.

// Each value in the configuration map contains the schema and a default value
// whose type matches the schema.
interface ConfigurationValue<TSchema extends z.ZodTypeAny> {
  schema: TSchema;
  defaultValue: z.infer<TSchema>;
}

// A map is built of flag keys to their variation types and a safe default value.
type TypedConfigurationMap<TSchemaMap extends Record<string, z.ZodTypeAny>> = {
  [TKey in keyof TSchemaMap]: ConfigurationValue<TSchemaMap[TKey]>;
};

// The flag fetch only accepts keys from the configuration map and returns
// the type mapped to that key.
interface FlagClient<TConfiguration extends TypedConfigurationMap<any>> {
  get<TKey extends keyof TConfiguration>(
    key: TKey,
  ): Promise<{ value: TConfiguration[TKey]["defaultValue"] }>;
}

The get refuses any key not in the map, so a schema must be defined to fetch its flag. This guarantees each flag is typed before it is used in code.

On the CI side, any breaking schema changes are blocked, preventing code changes that can't transition safely through LaunchDarkly. New code needs to read variation values from the previous schema, and old code needs to read variation values from the new schema.

Runtime type safety: parsing real variation values

Matching types helps development, but even if we avoid type casting, there is no guarantee the runtime value will match that type. The important added layer of protection comes from the fact that these types are inferred from Zod schemas, which we have available at runtime. Our SDK wrapper fetches the variation value, then parses it against the schema to ensure it's correct. This surfaces an error to the consumer when the value does not match the expected type, and a properly typed value when it does.

Type safety within LaunchDarkly: webhook approvals

So we had declared and runtime type safety. However (until recently: see JSON schemas below), we had no type safety in the external layer: there was no way to prevent bad values from being set in LaunchDarkly that did not match our expected type. We could still create values that cause runtime errors, even if the errors are now caught early in the SDK wrapper and can be monitored easily. We solved this with a validation service that receives and acts on webhooks. We configured LaunchDarkly to require approval for any flag changes, and it sent us a webhook for each approval request. There, we consumed these requests in our validation service and examined the payload.

The payload included the variation being changed, which we parsed against the flag's appropriate schema. If the change was valid, our service automatically approved and applied it, updating the variation immediately. Changes that didn't match the expected type were denied with a parsing failure to guide the requestor on where their attempt failed validation. This made it impossible to update a variation to put it out of sync with the flag's declared type. Or so we thought.

After working with the above solution for a while, issues and loopholes started to appear. For some, the automated approval process was confusing, and users didn't know to check their approval request for a denial to see whether their change actually landed. It also had maintenance costs, as changes in how LaunchDarkly handles webhooks and approvals caused validation issues. One limitation we found in our required approval flow was that we couldn't force approvals on newly created variations. A brand-new variation could be created invalid with no approval request and therefore no type safety. There are a few ways to work around this while maintaining the same overall structure, but we pivoted to a newly envisioned solution. We took advantage of a recent LaunchDarkly feature: JSON variation schemas.

Type safety within LaunchDarkly: JSON schemas

In June 2026, LaunchDarkly released a feature to validate that variations match a defined JSON schema. Leveraging this, we overhauled the external variation type validation process to be simpler and less brittle for everyone editing flags. With the JSON schemas, approval requests are unnecessary because changes that produce an invalid variation value cannot be saved.

However, this creates a new problem: the LaunchDarkly schema must be kept in sync with the Zod schema in code. To ease the development burden, we use skills that automate creating or updating a flag from a Zod schema. They invoke the Zod-to-JSON Schema conversion script and read the necessary business context. For creation, they post a type-safe flag in LaunchDarkly with all the metadata needed for ownership, maintenance, and eventual removal. For updates, they triage the flag and its variations, changing the schema if nonbreaking and surfacing the breaking variation issue to the developer otherwise. As another safeguard, we set up a cron job to regularly validate all our flags, ensuring each JSON flag has a variation schema that matches the Zod schema in code.

Looking forward

As we move further into agentic development lifecycles, it becomes more important to provide strict typing guidelines to maintain code quality as code is written faster with less human supervision. Feature flags provide a powerful lever to experiment with new features and prevent incidents in real time without code changes. But they often ended up deprecated, forgotten, or out of sync with reality, or they caused bugs themselves because they let production changes happen without oversight. They were avoided because of the boilerplate work required for setup and maintenance, both in code and through click ops in LaunchDarkly.

As development shifts to include agents that directly manage external connections (for example, having an agent create/update a flag on a developer's behalf), making the entire flag surface area type-safe becomes even more critical. Now, flags have a declared type that is enforced throughout the entire stack. Invalid usage of flags is a type error at compile time. CI enforces that changes to flag schemas are nonbreaking. Invalid variations or schema changes cannot be written in LaunchDarkly. At every read in production, we still check that the variation value matches the expected type. All of this while flag development and maintenance is handled agentically, requiring only input on the business context that needs to be covered. Using flags brings features and fixes to our users faster, and now flag development can keep up with the speed of modern development safely.