@tanstack/ai-code-mode
Version:
Code Mode for TanStack AI - LLM-driven code execution in secure sandboxes
24 lines (23 loc) • 1 kB
TypeScript
/**
* Strip TypeScript syntax from code, converting it to plain JavaScript.
*
* This is a safety net to ensure that even if an LLM generates TypeScript
* code with type annotations, it will be converted to valid JavaScript
* before being sent to the sandbox for execution.
*
* Uses esbuild's transform API which is extremely fast and handles all
* TypeScript syntax including:
* - Type annotations (: string, : number, etc.)
* - Generic types (Array<T>, Record<K, V>, etc.)
* - Interface and type declarations
* - Type assertions
* - Enums (converted to JavaScript objects)
*
* The code is wrapped in an async function before transformation to allow
* top-level `return` and `await` statements, then unwrapped after.
*
* @param code - TypeScript or JavaScript code
* @returns Plain JavaScript code with all type syntax removed
* @throws Error if esbuild fails (e.g., syntax error) or wrapper extraction fails
*/
export declare function stripTypeScript(code: string): Promise<string>;