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.
Problem
Following the integration's own documented usage produces a TypeScript compile error. Copy-pasting the snippet from the
growthbookIntegrationJSDoc / docs:fails to type-check against the real
@growthbook/growthbookclass:So a consumer who does exactly what the docs say is forced to add a cast — e.g.
growthbookClass: GrowthBook as unknown as ...— which is poor DX and directly contradicts the documented example. Reproduces on the latest release (10.62.0) via both@sentry/browserand@sentry/react.Root cause
The parameter is 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, andunknownis not assignable toOptions— sotypeof GrowthBookis rejected.This only surfaces through
@sentry/browser/@sentry/react.@sentry/coreannotates its export asIntegrationFn((...rest: any[])), which erases the parameter type and hides the problem;@sentry/browserre-declares the wrapper with the strict{ growthbookClass: GrowthBookClass }parameter (viasatisfies IntegrationFn), preserving the strict type that the public API exposes.Fix
The integration never constructs
growthbookClass— it only readsgrowthbookClass.prototypeto monkey-patchisOn/getFeatureValue. Typing the parameter as exactly that:accepts any class whose instances expose the wrapped surface (so the documented
typeof GrowthBookusage works 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).