or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli-commands.mdcreation.mdflow-versions.mdindex.mdinstallation.mdlibdefs.mdsearch.md
tile.json

creation.mddocs/

Definition Creation

Tools for creating new type definitions and stubs for libraries without existing definitions. This system provides both template generation for new definitions and automatic stub creation for missing libraries.

Capabilities

Template Definition Creation

Create template library definitions for new packages that need Flow type definitions.

/**
 * Create template library definitions in flow-typed repository format
 * @param args - Creation configuration options
 * @returns Promise resolving to exit code (0 for success)
 */
function createDef(args: CreateDefArgs): Promise<number>;

interface CreateDefArgs {
  /** Name of the library to create definition for */
  libName: string;
  /** Version range to add (e.g., "1.x.x", "^2.0.0") */
  ver: string;
}

Usage Examples:

import { createDef } from "flow-typed";

// Create definition template for a specific library
const result = await createDef({
  libName: "my-awesome-lib",
  ver: "1.x.x"
});

// Create definition for scoped package
const result = await createDef({
  libName: "@company/ui-components",
  ver: "2.x.x"
});

Stub Definition Creation

Create stub definitions for libraries that don't have existing Flow type definitions.

/**
 * Create stub definitions for missing libraries
 * Automatically generates basic type stubs based on the library's exports
 * @param args - Stub creation configuration options
 * @returns Promise resolving to exit code (0 for success)
 */
function createStub(args: CreateStubArgs): Promise<number>;

interface CreateStubArgs {
  /** Name of the library to create stub for */
  libName: string;
  /** Directory to install stub definitions (default: "flow-typed") */
  libdefDir?: string;
  /** Overwrite existing stub definitions */
  overwrite?: boolean;
  /** Maximum number of lines to include in stub */
  maxLines?: number;
}

Usage Examples:

import { createStub } from "flow-typed";

// Create basic stub for a library
const result = await createStub({
  libName: "unknown-package"
});

// Create stub with custom configuration
const result = await createStub({
  libName: "complex-lib",
  libdefDir: "custom-flow-typed",
  overwrite: true,
  maxLines: 100
});

Stub Utility Functions

Low-level utilities for working with stub generation and package analysis.

/**
 * Create stub definition for a package
 * @param projectRoot - Root directory of the project
 * @param packageName - Package name to create stub for
 * @param explicitVersion - Explicit version string or null
 * @param overwrite - Whether to overwrite existing stubs
 * @param pnpjs - PnP resolver or null
 * @param typescript - Whether to generate TypeScript stubs
 * @param libdefDir - Optional directory to create stub in
 * @param maxDepth - Optional maximum depth for stub generation
 * @returns Promise resolving to boolean indicating success
 */
function createStub(
  projectRoot: string,
  packageName: string,
  explicitVersion: string | null,
  overwrite: boolean,
  pnpjs: any | null,
  typescript: boolean,
  libdefDir?: string,
  maxDepth?: number
): Promise<boolean>;

/**
 * Check if a package has Flow files
 * @param pkgPath - Path to the package directory
 * @returns Promise resolving to boolean indicating if Flow files exist
 */
function pkgHasFlowFiles(pkgPath: string): Promise<boolean>;

interface PnpResolver {
  resolveRequest: Function;
  getMetadata: Function;
}

File Utilities for Definition Creation

Helper functions for file operations during definition creation.

/**
 * Copy file from source to destination
 * @param src - Source file path
 * @param dest - Destination file path
 * @returns Promise resolving when copy is complete
 */
function copyFile(src: string, dest: string): Promise<void>;

/**
 * Create directory recursively (like mkdir -p)
 * @param dirPath - Directory path to create
 * @returns Promise resolving when directory is created
 */
function mkdirp(dirPath: string): Promise<void>;

/**
 * Check if a file should be excluded from processing
 * @param filePath - File path to check
 * @returns Boolean indicating if file should be excluded
 */
function isExcludedFile(filePath: string): boolean;

Definition Template Structure

When creating template definitions, the system generates files with this structure:

Basic Template Format

// @flow

declare module 'package-name' {
  // Export declarations go here
  declare export default any;
  declare export function functionName(): any;
}

Versioned Template Format

// @flow

declare module 'package-name' {
  declare module.exports: {
    // Module exports go here
    +version: string,
    +default: any,
    // Add specific function signatures
  };
}

Stub Generation Process

The stub generation process follows these steps:

  1. Package Analysis: Analyze the target package to understand its structure
  2. Export Discovery: Identify the package's main exports and public API
  3. Flow Type Inference: Generate basic Flow type annotations based on detected patterns
  4. File Generation: Create the stub file with appropriate declarations
  5. Validation: Ensure the generated stub is syntactically valid

Stub Content Examples

Simple Package Stub:

// @flow

declare module 'simple-package' {
  declare module.exports: any;
}

Complex Package Stub:

// @flow

declare module 'complex-package' {
  declare export default {
    init(options?: Object): void,
    process(data: any): Promise<any>,
    configure(config: Object): void,
  };
  
  declare export var VERSION: string;
  declare export function helper(input: string): string;
}

Best Practices

Creating Quality Definitions

  • Use specific types instead of any when possible
  • Follow Flow's module declaration conventions
  • Include proper version constraints
  • Add comments for complex type structures

Stub Generation Guidelines

  • Review generated stubs and refine types manually
  • Use stubs as starting points, not final solutions
  • Consider contributing improved definitions back to flow-typed
  • Test stubs with actual usage patterns

Error Handling

  • Creation functions return exit codes (0 for success)
  • File system errors (permissions, disk space) are handled gracefully
  • Invalid package names or versions result in appropriate error messages
  • Existing definition conflicts are reported when overwrite is not enabled