fix(browser): Accept precisely-typed GrowthBook class in growthbookIntegration#21825
Open
codr wants to merge 1 commit into
Open
fix(browser): Accept precisely-typed GrowthBook class in growthbookIntegration#21825codr wants to merge 1 commit into
codr wants to merge 1 commit into
Conversation
…tegration
`growthbookIntegration({ growthbookClass: GrowthBook })` — the call shown in the
integration's own docs — did not type-check against the real
`@growthbook/growthbook` class, forcing consumers to cast. The parameter was
typed as `new (...args: unknown[]) => GrowthBook`, and a narrow constructor
(`constructor(options?: Options)`) is not assignable to an `unknown[]`
constructor.
The integration only reads `growthbookClass.prototype`, so type the parameter
structurally as `{ prototype: GrowthBook }`. This accepts a real GrowthBook
class with no cast, still rejects classes missing `isOn`/`getFeatureValue`, uses
no `any`, and makes the internal prototype cast redundant.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
growthbookIntegration({ growthbookClass: GrowthBook })— the exact call shown in the integration's own docs/JSDoc — does not type-check against the real@growthbook/growthbookGrowthBookclass, forcing consumers to cast.The parameter was typed as a constructor with
unknown[]args:GrowthBook's real constructor is
constructor(options?: Options). A constructor with a narrow parameter is not assignable to one declared with(...args: unknown[])(constructor parameters are contravariant:unknownis not assignable toOptions), sotypeof GrowthBookis rejected.@sentry/coremasks this by annotating the export asIntegrationFn((...rest: any[])), but@sentry/browserre-declares the wrapper with the strict{ growthbookClass: GrowthBookClass }parameter viasatisfies IntegrationFn, which preserves the strict type — so the public browser/react API requires a cast.Fix
The integration only ever reads
growthbookClass.prototypeto monkey-patchisOn/getFeatureValue; it never constructs the class. Typing the parameter as exactly that:accepts any class whose instances expose the wrapped surface (so
typeof GrowthBookworks with no cast), still rejects classes missingisOn/getFeatureValue, and avoidsany. It also makes the internalgrowthbookClass.prototype as GrowthBookLikecast redundant, which is removed.Test
Adds
packages/browser/test/integrations/featureFlags/growthbook/integration.test.ts: passes a mock class with a real, narrow constructor togrowthbookIntegrationwith no cast (the type regression guard) and asserts boolean evaluations are captured to the scope's flag context.yarn lint) & (yarn test).