TypeScript type definitions for React Native Community CLI configuration and command structures
—
Complete type definitions for React Native CLI command development with support for standard commands, detached commands, and command options with conditional typing.
Type-safe command function signatures for both standard and detached command modes.
/**
* Standard command function signature
* @param argv - Command line arguments array
* @param ctx - Complete CLI configuration context
* @param args - Parsed command arguments
*/
type CommandFunction<Args = Object> = (
argv: Array<string>,
ctx: Config,
args: Args,
) => Promise<void> | void;
/**
* Detached command function signature with different parameter order
* @param argv - Command line arguments array
* @param args - Parsed command arguments
* @param ctx - Complete CLI configuration context
*/
type DetachedCommandFunction<Args = Object> = (
argv: string[],
args: Args,
ctx: Config,
) => Promise<void> | void;Usage Examples:
import { CommandFunction, DetachedCommandFunction, Config } from "@react-native-community/cli-types";
// Standard command
const buildCommand: CommandFunction = async (argv, ctx, args) => {
console.log(`Building project at ${ctx.root}`);
console.log(`React Native version: ${ctx.reactNativeVersion}`);
if (ctx.project.android) {
console.log(`Android app: ${ctx.project.android.appName}`);
}
};
// Detached command (different parameter order)
const detachedCommand: DetachedCommandFunction = async (argv, args, ctx) => {
console.log(`Detached command with args:`, args);
console.log(`Project root: ${ctx.root}`);
};Configuration options for command-line arguments with parsing and default value support.
/**
* Command option configuration
* @template T - Type of default value function
*/
type CommandOption<T = (ctx: Config) => OptionValue> = {
/** Option name (e.g., "--verbose" or "-v") */
name: string;
/** Human-readable description for help text */
description?: string;
/** Function to parse string value into desired type */
parse?: (val: string) => any;
/** Default value or function that returns default value */
default?: OptionValue | T;
};
type OptionValue = string | boolean | number;Usage Examples:
import { CommandOption, Config } from "@react-native-community/cli-types";
// Static default value
const verboseOption: CommandOption = {
name: "--verbose",
description: "Enable verbose logging",
default: false
};
// Dynamic default based on config
const platformOption: CommandOption<(ctx: Config) => string> = {
name: "--platform",
description: "Target platform",
parse: (val: string) => val.toLowerCase(),
default: (ctx: Config) => ctx.project.android ? "android" : "ios"
};
// Custom parser
const countOption: CommandOption = {
name: "--count",
description: "Number of iterations",
parse: (val: string) => parseInt(val, 10),
default: 1
};Complete command definitions with conditional typing for detached mode and comprehensive metadata support.
/**
* Complete command definition with conditional typing
* @template IsDetached - Whether this is a detached command
*/
type Command<IsDetached extends boolean = false> = {
/** Command name as used in CLI */
name: string;
/** Human-readable description */
description?: string;
/** Whether this is a detached command */
detached?: IsDetached;
/** Usage examples for help text */
examples?: Array<{
desc: string;
cmd: string;
}>;
/** Package information */
pkg?: {
name: string;
version: string;
};
/** Command implementation function */
func: IsDetached extends true
? DetachedCommandFunction<Object>
: CommandFunction<Object>;
/** Command-line options */
options?: Array<
CommandOption<
IsDetached extends true ? () => OptionValue : (ctx: Config) => OptionValue
>
>;
};
/**
* Type alias for detached commands
*/
type DetachedCommand = Command<true>;Usage Examples:
import { Command, CommandFunction, DetachedCommand, DetachedCommandFunction } from "@react-native-community/cli-types";
// Standard command
const startCommand: Command = {
name: "start",
description: "Start the React Native development server",
examples: [
{
desc: "Start with default configuration",
cmd: "npx react-native start"
},
{
desc: "Start on specific port",
cmd: "npx react-native start --port 8082"
}
],
func: async (argv, ctx, args) => {
console.log("Starting development server...");
},
options: [
{
name: "--port",
description: "Port to run server on",
parse: (val: string) => parseInt(val, 10),
default: 8081
},
{
name: "--reset-cache",
description: "Reset bundler cache",
default: false
}
]
};
// Detached command
const detachedBuildCommand: DetachedCommand = {
name: "build",
description: "Build the application",
detached: true,
pkg: {
name: "@react-native-community/cli-build",
version: "1.0.0"
},
func: async (argv, args, ctx) => {
console.log("Building application...");
},
options: [
{
name: "--release",
description: "Build for release",
default: () => false // Note: detached commands use () => OptionValue
}
]
};How commands integrate with the broader CLI configuration system.
// Commands are part of the main Config interface
interface Config {
// ... other properties
commands: Command[];
// ... other properties
}
// User dependency configurations can also provide commands
type UserDependencyConfig = {
// ... other properties
commands: Command[];
// ... other properties
};Usage Examples:
import { Config, Command } from "@react-native-community/cli-types";
// Adding commands to configuration
function addCustomCommand(config: Config, newCommand: Command): Config {
return {
...config,
commands: [...config.commands, newCommand]
};
}
// Finding commands by name
function findCommand(config: Config, name: string): Command | undefined {
return config.commands.find(cmd => cmd.name === name);
}
// Filtering detached commands
function getDetachedCommands(config: Config): DetachedCommand[] {
return config.commands.filter(cmd => cmd.detached === true) as DetachedCommand[];
}Install with Tessl CLI
npx tessl i tessl/npm-react-native-community--cli-types