Type definitions for the PostHog JavaScript SDK
npm install @posthog/typesType definitions for the PostHog JavaScript SDK.
You're loading PostHog via a tag and want TypeScript types for window.posthog.
``html`
You're installing any PostHog library via npm/yarn/pnpm. The types are already included:
- posthog-js - Browser SDK (includes all types)posthog-node
- - Node.js SDKposthog-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
`
`bash`
npm install @posthog/typesor
yarn add @posthog/typesor
pnpm add @posthog/types
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') {
// ...
}
`
`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',
}
`
This package's version is synchronized with posthog-js`. They are always released together with matching version numbers.
MIT