> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt
# TypeScript types for script tag installation
Canonical URL: https://posthog.com/docs/libraries/js/types
> **Cloud region:** This page uses US Cloud hosts. For an EU Cloud project, use `https://eu.i.posthog.com` instead of `https://us.i.posthog.com` (ingestion host).
## On this page
- [When to use this package](#when-to-use-this-package)
- [Installation](#installation)
- [Usage](#usage)
- [Version synchronization](#version-synchronization)
If you're loading PostHog via a `
```
### You don't need this package if:
You're installing any Javascript/Typescript PostHog library via npm, yarn, pnpm, or bun. The types are **already included**:
| Package | Description |
| --- | --- |
| `posthog-js` | Browser SDK |
| `posthog-js-lite` | Lighter browser SDK (types are different) |
| `posthog-node` | Node.js SDK |
| `posthog-react-native` | React Native SDK |
| `@posthog/react` | React hooks and components |
```typescript
// Types are already available when you install posthog-js
import posthog from 'posthog-js'
posthog.init('your-api-key')
posthog.capture('my_event') // ✅ Fully typed
```
## Installation
```bash
npm install @posthog/types@1
# or
yarn add @posthog/types@1
# or
pnpm add @posthog/types@1
# or
bun add @posthog/types@1
```
## Usage
### Typing `window.posthog` (script tag usage)
Create a type declaration file to type `window.posthog`:
```typescript
// posthog.d.ts
import type { PostHog } from '@posthog/types'
declare global {
interface Window {
posthog?: PostHog
}
}
export {}
```
Now you can use `window.posthog` with full type safety:
```typescript
// Your code
window.posthog?.capture('button_clicked', { button_id: 'signup' })
window.posthog?.identify('user-123', { email: 'user@example.com' })
const flagValue = window.posthog?.getFeatureFlag('my-flag')
if (flagValue === 'variant-a') {
// ...
}
```
### Typing configuration objects
You can also use the types to ensure your configuration objects are correctly typed:
```typescript
import type { PostHogConfig, Properties } from '@posthog/types'
// Type your configuration
const config: Partial = {
api_host: 'https://us.i.posthog.com',
autocapture: true,
capture_pageview: 'history_change',
}
// Type event properties
const eventProps: Properties = {
button_id: 'signup',
page: '/pricing',
}
```
## Version synchronization
The `@posthog/types` package version is synchronized with `posthog-js`. They are always released together with matching version numbers, ensuring type definitions stay in sync with the SDK.