Skip to content
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

refactor: Simplify Posthog.init() signature #1712

Merged
merged 1 commit into from
Feb 4, 2025
Merged

Conversation

rafaeelaudibert
Copy link
Member

In a recent version, we updated the body of this function to always return a Posthog instance but we forgot to update the type, let's fix that

This is a slight type-level breaking change at posthog-core.ts. This is more serious than the recent changes we did to code that was only present in the React SDK. Should we ship this? I assume you'll have thoughts @pauldambra

Originally written by https://github.com/thexeos but I couldn't get his PR to pass CI - secrets are not shared with contributors PRs

Closes #1616
Supersedes #1617

@rafaeelaudibert rafaeelaudibert added the bump patch Bump patch version when this PR gets merged label Feb 3, 2025
@rafaeelaudibert rafaeelaudibert requested review from pauldambra and a team February 3, 2025 14:33
Copy link

vercel bot commented Feb 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Feb 4, 2025 1:14pm

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR updates the TypeScript signature of PostHog's init() method to correctly reflect that it always returns a PostHog instance, removing the misleading | undefined from the return type.

Key changes:

  • Modified return type of init() in src/posthog-core.ts from PostHog | undefined to PostHog
  • Aligns TypeScript types with actual implementation behavior which already returns an instance in all cases
  • Type-level breaking change that improves type safety and developer experience

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you'll have thoughts @pauldambra

🤣 i love my js sdk, don't hurt them!


I think this is a safe direction to make the change. You're either already ignoring that this could be undefined or handling it.

At least now we'd never return undefined. Potentially as with the previous change someone's TS breaks

e.g. if they have const x: PostHog | undefined = init(blah) then TS might complain

but importantly i think this is safe in the running code!

Copy link

github-actions bot commented Feb 3, 2025

Size Change: 0 B

Total Size: 3.3 MB

ℹ️ View Unchanged
Filename Size
dist/all-external-dependencies.js 215 kB
dist/array.full.es5.js 268 kB
dist/array.full.js 371 kB
dist/array.full.no-external.js 369 kB
dist/array.js 183 kB
dist/array.no-external.js 181 kB
dist/customizations.full.js 13.8 kB
dist/dead-clicks-autocapture.js 14.5 kB
dist/exception-autocapture.js 9.48 kB
dist/external-scripts-loader.js 2.64 kB
dist/main.js 184 kB
dist/module.full.js 371 kB
dist/module.full.no-external.js 369 kB
dist/module.js 183 kB
dist/module.no-external.js 181 kB
dist/recorder-v2.js 115 kB
dist/recorder.js 115 kB
dist/surveys-preview.js 69.4 kB
dist/surveys.js 72.4 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

In a recent version we updated the body of this function to always return a `Posthog` instance but we forgot to update the type, let's fix that

Originally written by https://github.com/thexeos but I couldn't get his PR to pass CI - secrets are not shared with contributors PRs

Closes #1616

Co-authored by thexeos <teodor@massluminosity.com>
@rafaeelaudibert rafaeelaudibert merged commit 764c9cb into main Feb 4, 2025
28 checks passed
@rafaeelaudibert rafaeelaudibert deleted the fix-1616 branch February 4, 2025 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump patch Bump patch version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PostHog.init() always returns a value, TS types are misleading
2 participants