@posthog/types
Type definitions for the PostHog JavaScript SDK.
When to Use This Package
✅ You Need This Package If:
You're loading PostHog via a <script> tag and want TypeScript types for window.posthog.
1<!-- You load PostHog like this -->
2<script>
3 !function(t,e){...}(document,window.posthog||[]);
4 posthog.init('your-api-key', { api_host: 'https://us.i.posthog.com' })
5</script>
❌ You Don't Need This Package If:
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
1// Types are already available when you install posthog-js
2import posthog from 'posthog-js'
3
4posthog.init('your-api-key')
5posthog.capture('my_event') // ✅ Fully typed
Installation
1npm install @posthog/types
2# or
3yarn add @posthog/types
4# or
5pnpm add @posthog/types
Usage
Typing window.posthog (Script Tag Usage)
Create a type declaration file to type window.posthog:
1// posthog.d.ts
2import type { PostHog } from '@posthog/types'
3
4declare global {
5 interface Window {
6 posthog?: PostHog
7 }
8}
9
10export {}
Now you can use window.posthog with full type safety:
1// Your code
2window.posthog?.capture('button_clicked', { button_id: 'signup' })
3window.posthog?.identify('user-123', { email: 'user@example.com' })
4
5const flagValue = window.posthog?.getFeatureFlag('my-flag')
6if (flagValue === 'variant-a') {
7 // ...
8}
Typing Configuration Objects
1import type { PostHogConfig, Properties } from '@posthog/types'
2
3// Type your configuration
4const config: Partial<PostHogConfig> = {
5 api_host: 'https://us.i.posthog.com',
6 autocapture: true,
7 capture_pageview: 'history_change',
8}
9
10// Type event properties
11const eventProps: Properties = {
12 button_id: 'signup',
13 page: '/pricing',
14}
Version Synchronization
This package's version is synchronized with posthog-js. They are always released together with matching version numbers.
License
MIT