Ben Fornefeld 8c43806f81 Fix: TypeScript union type resolution for Commands.run() method (#753)
## Fix TypeScript union type resolution for Commands.run() method

### What's Changed
Improved TypeScript type safety for the `Commands.run()` method by
adding proper function overloads to handle the union case when the
`background` parameter type is not known at compile time.

### Key Improvements

**Better Type Safety**
- Added a third function overload to correctly handle the `background?:
boolean` union case
- TypeScript now correctly infers return types:
  - `background: true` → returns `CommandHandle`
  - `background: false | undefined` → returns `CommandResult`
- `background: boolean` (unknown at compile time) → returns
`CommandHandle | CommandResult`

**Enhanced Developer Experience**
- Added comprehensive JSDoc overloads that clearly document each usage
pattern
- IntelliSense now provides accurate type hints and autocompletion
- Eliminates ambiguous union types when the `background` value is known
at compile time

**Prevents Runtime Errors**
- When `background` is a literal value, developers get precise types and
can't accidentally call wrong methods
- When `background` is a runtime boolean, TypeScript correctly shows the
union type requiring proper type narrowing

### Why This Matters for SDK Users

Before this change, developers had to ignore ts warning & manually cast
union types when using `commands.run()`:
```typescript
// Before: typescript was complaining, because it expected `true` or `false` as literals. 
// Required manual casting:

// @ts-ignore
const result = await sbx.commands.run('ls', { background: isBackground }) as CommandHandle | CommandResult
```

After this change, TypeScript automatically infers the correct type:
```typescript
// After: TypeScript knows this is CommandHandle
const handle = await sbx.commands.run('ls', { background: true })
await handle.wait() //  TypeScript knows .wait() is available

// TypeScript knows this is CommandResult  
const result = await sbx.commands.run('ls')
console.log(result.stdout) //  TypeScript knows .stdout is available

// When background value is unknown at compile time, returns union type
const isBackground: boolean = Math.random() > 0.5
const result = await commands.run('ls', { background: isBackground })
// result is CommandHandle | CommandResult - requires type narrowing or casting
```
2025-06-03 10:09:31 +00:00
2024-10-15 11:52:54 -07:00
2025-03-22 11:21:55 +01:00
2025-01-23 21:17:08 -08:00
2024-10-15 08:29:46 -07:00
2024-10-15 10:39:10 -07:00
2024-10-15 11:52:54 -07:00
2025-03-13 03:36:40 -07:00
2023-07-01 17:08:01 -07:00
2023-05-21 10:51:33 -07:00
2025-03-22 11:21:55 +01:00

E2B SDK Preview E2B SDK Preview

Last 1 month downloads for the Python SDK Last 1 month downloads for the JavaScript SDK

What is E2B?

E2B is an open-source infrastructure that allows you to run AI-generated code in secure isolated sandboxes in the cloud. To start and control sandboxes, use our JavaScript SDK or Python SDK.

Note

This repository contains the core E2B SDK that's used in our main E2B Code Interpreter SDK.

Run your first Sandbox

1. Install SDK

JavaScript / TypeScript

npm i @e2b/code-interpreter

Python

pip install e2b-code-interpreter

2. Get your E2B API key

  1. Sign up to E2B here.
  2. Get your API key here.
  3. Set environment variable with your API key
E2B_API_KEY=e2b_***

3. Execute code with code interpreter inside Sandbox

JavaScript / TypeScript

import { Sandbox } from '@e2b/code-interpreter'

const sandbox = await Sandbox.create()
await sandbox.runCode('x = 1')

const execution = await sandbox.runCode('x+=1; x')
console.log(execution.text)  // outputs 2

Python

from e2b_code_interpreter import Sandbox

with Sandbox() as sandbox:
    sandbox.run_code("x = 1")
    execution = sandbox.run_code("x+=1; x")
    print(execution.text)  # outputs 2

4. Check docs

Visit E2B documentation.

5. E2B cookbook

Visit our Cookbook to get inspired by examples with different LLMs and AI frameworks.

Self-hosting

Read the self-hosting guide to learn how to set up the E2B infrastructure on your own. The infrastructure is deployed using Terraform.

Supported cloud providers:

  • 🟢 GCP
  • 🚧 AWS
  • Azure
  • General linux machine
S
Description
E2B 是开源的安全运行时环境,为企业级 AI Agent 提供真实工具支持。|GitHub 镜像 13.6k · 🍴 1k
https://github.com/e2b-dev/e2b Readme Apache-2.0 129 MiB
Languages
Python 57.1%
TypeScript 42.2%
Dockerfile 0.3%
JavaScript 0.1%
Makefile 0.1%
Other 0.1%