Edge Runtime Types provides TypeScript global type definitions for the Edge Runtime environment. This package makes Edge Runtime-specific APIs available globally in TypeScript projects without explicit imports, enabling type-safe development of serverless functions and edge applications.
npm install @edge-runtime/types --save-devThis package provides global type definitions that extend the TypeScript global scope. No explicit imports are required - simply install the package and the global types become available:
/// <reference types="@edge-runtime/types" />
// Global addEventListener is now typed for Edge Runtime
addEventListener('fetch', (event) => {
// event is automatically typed as Edge.FetchEvent
const { request } = event;
event.respondWith(new Response('Hello'));
});
// Global constructors are available with proper typing
const pattern = new URLPattern('/api/*');
const fetchEvent = new FetchEvent(request);To access Edge Runtime primitives directly in your code:
import * as Edge from '@edge-runtime/primitives';// TypeScript configuration (tsconfig.json)
{
"compilerOptions": {
"types": ["@edge-runtime/types"]
}
}
// In your Edge Runtime function
addEventListener('fetch', (event: FetchEvent) => {
const { request } = event;
// event and request are fully typed
event.respondWith(
new Response('Hello from Edge Runtime', {
status: 200,
headers: { 'Content-Type': 'text/plain' }
})
);
});
// Global EdgeRuntime identifier is available
console.log(typeof EdgeRuntime); // "object"Provides type-safe addEventListener function specifically for Edge Runtime's 'fetch' events.
/**
* Adds an event listener for 'fetch' events in Edge Runtime
* @param type - Event type, must be 'fetch'
* @param listener - Event handler receiving Edge.FetchEvent
*/
function addEventListener(
type: 'fetch',
listener: (event: Edge.FetchEvent) => void,
): void;Edge Runtime identifier and global scope typing.
/**
* Edge Runtime identifier constant (empty object)
*/
const EdgeRuntime: Record<never, never>;
/**
* Global object typed as Edge Runtime primitives namespace
* Makes all Edge Runtime primitives available globally
*/
const globalThis: typeof Edge;Direct access to Edge Runtime constructors in the global scope.
/**
* FetchEvent constructor reference from Edge Runtime primitives
*/
const FetchEvent: typeof Edge.FetchEvent;
/**
* URLPattern constructor reference from Edge Runtime primitives
*/
const URLPattern: typeof Edge.URLPattern;This package depends on @edge-runtime/primitives and makes its types available globally. The actual API implementations (FetchEvent, URLPattern, etc.) are provided by the primitives package, while this package handles the global type declarations.
/**
* This package imports from @edge-runtime/primitives and declares global types
*/
import * as Edge from '@edge-runtime/primitives';
// Global declarations make Edge primitives available without imports
declare global {
function addEventListener(
type: 'fetch',
listener: (event: Edge.FetchEvent) => void,
): void;
const EdgeRuntime: Record<never, never>;
const globalThis: typeof Edge;
const FetchEvent: typeof Edge.FetchEvent;
const URLPattern: typeof Edge.URLPattern;
}This package provides global TypeScript type definitions by extending the global scope. When installed, the following types become available:
All other Edge Runtime APIs (Request, Response, crypto, streams, etc.) are available through the @edge-runtime/primitives dependency and are accessible globally when using this types package.
To use this package, add it to your TypeScript configuration:
{
"compilerOptions": {
"types": ["@edge-runtime/types"],
"lib": ["ES2019"]
}
}Or reference it directly in your TypeScript files:
/// <reference types="@edge-runtime/types" />@edge-runtime/primitives (automatically installed)