Overview
The @flagsync/cli generates TypeScript type definitions based on your flag configurations.
npm Latest version available on npm
GitHub Explore the source code on GitHub
Installation
Install the CLI with your preferred package manager:
npm install -D @flagsync/cli
yarn add -D @flagsync/cli
pnpm add -D @flagsync/cli
Quickstart
Authenticate with your FlagSync account:
Generate type definitions:
$ npx flagsync generate
✓ Select your SDK: React
i Found 4 flags
✓ Generated flag types, saved to "gen/flags.d.ts"
Use in your code:
import React from 'react' ;
import { useFlag } from '@flagsync/react-sdk' ;
function MyComponent () {
// TypeScript knows this returns a boolean
const { value : isEnabled } = useFlag ( 'show-new-feature' );
// TypeScript knows this returns "light" | "dark" | "auto"
const { value : theme } = useFlag ( 'theme-preference' );
return (
< div className = { theme } >
{ isEnabled && < NewFeatureComponent />}
</ div >
);
}
The generated types will be saved to gen/flags.d.ts. Most default tsconfig.json setups include this file via a broad pattern like **/*.ts, but if you’re using a custom config, make sure it’s covered by the include array: "include": ["**/*.ts"]
Generated Types
The CLI generates TypeScript types based on your flag configurations.
Boolean Flags
String Flags
Number Flags
JSON Flags
// For boolean flags
declare module "@flagsync/node-sdk" {
export interface FeatureFlags {
"enable-dark-mode" : boolean ;
"show-beta-features" : boolean ;
}
}
// For string flags with variants
declare module "@flagsync/react-sdk" {
export interface FeatureFlags {
"theme-color" : "blue" | "red" | "green" ;
"welcome-message" : "hello" | "welcome" | "hi there" ;
}
}
// For number flags with variants
declare module "@flagsync/nestjs-sdk" {
export interface FeatureFlags {
"max-items" : 10 | 25 | 50 | 100 ;
"timeout-duration" : 1000 | 5000 | 10000 ;
}
}
// For JSON/object flags
declare module "@flagsync/nextjs-sdk" {
export interface FeatureFlags {
"config-object" : object ;
"feature-settings" : object ;
}
}
Integration Examples
A few integration examples for FlagSync’s SDKS. Supported SDKs:
@flagsync/react-sdk
import React from 'react' ;
import { useFlag } from '@flagsync/react-sdk' ;
function MyComponent () {
// TypeScript knows this returns a boolean
const { value : isEnabled } = useFlag ( 'show-new-feature' );
// TypeScript knows this returns "light" | "dark" | "auto"
const { value : theme } = useFlag ( 'theme-preference' );
return (
< div className = { theme } >
{ isEnabled && < NewFeatureComponent />}
</ div >
);
}
@flagsync/nextjs-sdk
import { welcomeMessage } from '@/lib/flagsync' ;
export default function HomePage () {
// TypeScript knows this returns "hello" | "welcome" | "hi there"
const message = await welcomeMessage ();
return < h1 > { message } </ div > ;
}
import { flag } from 'flags/next' ;
import { getFlag } from '@/lib/flagsync/flag' ;
// TypeScript knows this returns "hello" | "welcome" | "hi there"
export const welcomeMessage = flag ({
key: 'welcome-message' ,
async decide ( params ) {
return getFlag ( this . key , params );
},
});
@flagsync/js-sdk
import { FlagSyncFactory } from '@flagsync/js-sdk' ;
// Initialize the SDK
const factory = FlagSyncFactory ({ ... });
const client = factory . client ()
await client . waitForReady ();
// TypeScript knows this returns 10 | 25 | 50 | 100
const maxItems = client . flag ( 'max-items' );
Commands
flagsync login
Authenticates the CLI with your FlagSync account using a secure OAuth2 flow.
Opens your browser for authentication, then walks you through selecting your project context
Prompts:
Organization : Choose which FlagSync organization to use
Workspace : Select the workspace containing your flags
flagsync logout
Disconnects the CLI from your FlagSync account.
Clears your saved login credentials. You’ll need to run flagsync login again to use other commands.
flagsync generate
Creates TypeScript definitions for your feature flags.
Pulls your flag definitions and generates type-safe TypeScript code in gen/flags.d.ts.
Prompts:
SDK Selection : Choose your framework:
File Structure
After running flagsync generate, your project will have:
your-project/
├── gen/
│ └── flags.d.ts # Generated type definitions
├── package.json
└── ... your other files
The generated flags.d.ts file contains:
Module declaration for your chosen SDK
FeatureFlags interface with all your flags
Type-safe flag key constants
The generated types will be saved to gen/flags.d.ts. Most default tsconfig.json setups include this file via a broad pattern like **/*.ts, but if you’re using a custom config, make sure it’s covered by the include array: "include": ["**/*.ts"]