Skip to content

TypeScript Scripts

TypeScript scripts are compiled to JavaScript using tsc before being passed to the Node.js sandbox. The same sandbox restrictions as JavaScript apply — only fetch, logger, authHeaders, datasources, and payload are available.

Type definitions

The editor provides IntelliSense for the injected globals. You can use types directly without importing anything:

typescript
// authHeaders is typed as Record<string, string>
const headers: Record<string, string> = authHeaders

// fetch is the standard Web Fetch API
const response: Response = await fetch('https://api.example.com/data', {
  headers
})

Example: typed API response

typescript
interface Order {
  id: string
  status: 'pending' | 'complete' | 'cancelled'
  total: number
  createdAt: string
}

const response = await fetch('https://api.example.com/orders', {
  headers: authHeaders
})

if (!response.ok) {
  logger.error(`API error: ${response.status} ${response.statusText}`)
  throw new Error(`HTTP ${response.status}`)
}

const orders: Order[] = await response.json()
const pending = orders.filter(o => o.status === 'pending')

logger.info(`Total orders: ${orders.length}, pending: ${pending.length}`)
logger.warn(`Pending order IDs: ${pending.map(o => o.id).join(', ')}`)

console.log(JSON.stringify(pending, null, 2))

Example: async helper functions

typescript
async function fetchPage(page: number): Promise<{ items: unknown[]; total: number }> {
  const res = await fetch(`https://api.example.com/items?page=${page}`, {
    headers: authHeaders
  })
  if (!res.ok) throw new Error(`Page ${page} failed: ${res.status}`)
  return res.json()
}

const first = await fetchPage(1)
logger.info(`Total items: ${first.total}`)

const allItems: unknown[] = [...first.items]
const pages = Math.ceil(first.total / first.items.length)

for (let p = 2; p <= pages; p++) {
  const page = await fetchPage(p)
  allItems.push(...page.items)
}

logger.info(`Fetched all ${allItems.length} items`)

Compilation errors

If the TypeScript compiler finds type errors, the run is marked Failed before execution begins. The compilation error is shown in the run log. Fix the type error and run again.

TIP

Use the Test button in the task editor to catch TypeScript errors quickly without creating a run history entry.