TypeScript

Declare your flag keys once to get autocomplete and a compile error for typos in useFlag, getFlag and isEnabled.

By default a flag key is any string, so a typo like useFlag('new-chekout') compiles and quietly returns its fallback. Tell the SDK which flags you have and TypeScript catches it instead.

Declare your flags

Add a file anywhere your tsconfig.json includes, listing your flag names exactly as they appear in the dashboard:

flaggish.d.ts
import '@flaggish/sdk';

declare module '@flaggish/sdk' {
  interface Register {
    flags: 'new-checkout' | 'search-v2';
  }
}

That’s it. useFlag, getFlag, isEnabled and defaults now autocomplete your flag names, and anything else is a compile error:

Checkout.tsx
useFlag('new-checkout'); // ✓
useFlag('new-chekout'); // ✗ not assignable to 'new-checkout' | 'search-v2'

It works whichever entry point you import from: @flaggish/sdk, @flaggish/sdk/react or @flaggish/sdk/next. Without the file, keys stay plain strings, so nothing changes until you add it.

Keep it up to date

When you create a flag in the dashboard, add its name to the list. When you delete one, remove it, and TypeScript points you at every place that still checks it.

For a key you build at runtime, cast it: useFlag(name as FlagKey), with FlagKey imported from the SDK.

Typos without types

Outside production (when NODE_ENV isn’t production), the SDK also warns in the console the first time you check a key that isn’t in your flags:

Console
[flaggish] Unknown flag "new-chekout": there is no flag with this key in this environment, so it uses its fallback. Check the spelling, or create the flag in the dashboard.

It warns once per key, only after your flags have loaded, and never in production. A flag that exists but is switched off doesn’t trigger it.

Was this page helpful?

Request early access

Become a beta user, or stay up to date with the project.