-
Couldn't load subscription status.
- Fork 195
feat: event schema types #2445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: event schema types #2445
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "name": "posthog-js", | ||
| "version": "1.275.1", | ||
| "version": "1.275.2", | ||
| "description": "Posthog-js allows you to automatically capture usage and send events to PostHog.", | ||
| "repository": "https://github.com/PostHog/posthog-js", | ||
| "author": "[email protected]", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * Typed event capture infrastructure for PostHog. | ||
| * | ||
| * Event schemas can be augmented in PostHogEventSchemas interface via module augmentation. | ||
| * This is typically done by generated types from `posthog-cli schema pull`. | ||
| * | ||
| * @example | ||
| * // In your types file or generated file: | ||
| * declare module '@posthog/core' { | ||
| * interface PostHogEventSchemas { | ||
| * 'user_signed_up': { plan: string; trial: boolean } | ||
| * 'purchase_completed': { amount: number; currency: string } | ||
| * } | ||
| * } | ||
| * | ||
| * // Usage: | ||
| * posthog.typed.user_signed_up({ plan: 'pro', trial: true }) | ||
| * posthog.typed.purchase_completed({ amount: 99.99, currency: 'USD' }) | ||
| */ | ||
|
|
||
| import type { PostHogEventSchemas } from './types' | ||
|
|
||
| // Utility type that allows schema properties plus any additional properties | ||
| // The schema properties are strictly typed, additional ones are any | ||
| export type EventWithAdditionalProperties<T> = T & Record<string, any> | ||
|
|
||
| /** | ||
| * Mapped type that creates typed methods for each event in PostHogEventSchemas. | ||
| * Methods are generated dynamically based on the augmented interface. | ||
| */ | ||
| export type TypedEventCapture<Client extends { capture: (event: string, properties?: any) => any }> = { | ||
| [K in keyof PostHogEventSchemas]: ( | ||
| properties: EventWithAdditionalProperties<PostHogEventSchemas[K]> | ||
| ) => ReturnType<Client['capture']> | ||
| } | ||
|
|
||
| /** | ||
| * Creates a Proxy that dynamically generates typed event methods | ||
| * based on the PostHogEventSchemas interface. | ||
| */ | ||
| export function createTypedEventCapture<Client extends { capture: (event: string, properties?: any) => any }>( | ||
| client: Client | ||
| ): TypedEventCapture<Client> { | ||
| return new Proxy({} as TypedEventCapture<Client>, { | ||
| get: (_target, eventName: string) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: The Prompt To Fix With AIThis is a comment left during a code review.
Path: packages/core/src/typed-events.ts
Line: 45:45
Comment:
**logic:** The `eventName` parameter should be typed as `string | symbol` since Proxy get traps can receive Symbols (e.g., for well-known symbols like `Symbol.toStringTag`). Consider adding a type guard to filter out non-string property keys.
How can I resolve this? If you propose a fix, please make it concise. |
||
| return (properties: any) => { | ||
| return client.capture(eventName, properties) | ||
| } | ||
| }, | ||
| }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic:
Proxyis not supported in IE 11, which is listed in the browserslist. This will cause runtime errors in IE 11 environments when thetypedproperty is accessed.Prompt To Fix With AI