Skip to content

Commit 4320966

Browse files
committed
fix: Narrow extension types as non-optional for full bundle
This ensures 100% backwards compatibility
1 parent 13f9506 commit 4320966

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import './default-extensions'
22
import './external-scripts-loader'
3-
import { init_as_module } from '../posthog-core'
4-
export { PostHog } from '../posthog-core'
3+
import { init_as_module, PostHog as _PostHog, type PostHogWithExtensions } from '../posthog-core'
4+
5+
// In the full bundle, tree-shakeable extensions are always loaded,
6+
// so we narrow the type to make them non-optional.
7+
export const PostHog = _PostHog as typeof _PostHog &
8+
(new (...args: ConstructorParameters<typeof _PostHog>) => PostHogWithExtensions)
9+
export type PostHog = PostHogWithExtensions
10+
511
export * from '../types'
612
export * from '../posthog-surveys-types'
713
export * from '../posthog-product-tours-types'
814
export * from '../posthog-conversations-types'
9-
export const posthog = init_as_module()
15+
export const posthog = init_as_module() as PostHogWithExtensions
1016
export default posthog
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import './default-extensions'
2-
import { init_as_module } from '../posthog-core'
3-
export { PostHog } from '../posthog-core'
2+
import { init_as_module, PostHog as _PostHog, type PostHogWithExtensions } from '../posthog-core'
3+
4+
// In the full bundle, tree-shakeable extensions are always loaded,
5+
// so we narrow the type to make them non-optional.
6+
export const PostHog = _PostHog as typeof _PostHog & (new (...args: ConstructorParameters<typeof _PostHog>) => PostHogWithExtensions)
7+
export type PostHog = PostHogWithExtensions
8+
49
export * from '../types'
510
export * from '../posthog-surveys-types'
611
export * from '../posthog-product-tours-types'
712
export * from '../posthog-conversations-types'
8-
export const posthog = init_as_module()
13+
export const posthog = init_as_module() as PostHogWithExtensions
914
export default posthog

packages/browser/src/posthog-core.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,6 +3889,13 @@ export function init_from_snippet(): void {
38893889
add_dom_loaded_handler()
38903890
}
38913891

3892+
/**
3893+
* When using the full bundle, all tree-shakeable extensions are guaranteed to be available.
3894+
* The slim bundle keeps them optional.
3895+
*/
3896+
export type PostHogWithExtensions = PostHog &
3897+
Required<Pick<PostHog, 'surveys' | 'conversations' | 'logs' | 'experiments' | 'toolbar' | 'exceptions'>>
3898+
38923899
export function init_as_module(): PostHog {
38933900
const posthogMain = (instances[PRIMARY_INSTANCE_NAME] = new PostHog())
38943901

0 commit comments

Comments
 (0)