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

fix(core): Export Scope interface as Scope #12067

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const DEFAULT_MAX_BREADCRUMBS = 100;
/**
* Holds additional event information.
*/
export class Scope implements ScopeInterface {
class ScopeClass implements ScopeInterface {
/** Flag if notifying is happening. */
protected _notifyingListeners: boolean;

Expand Down Expand Up @@ -116,8 +116,8 @@ export class Scope implements ScopeInterface {
/**
* @inheritDoc
*/
public clone(): Scope {
const newScope = new Scope();
public clone(): ScopeClass {
const newScope = new ScopeClass();
newScope._breadcrumbs = [...this._breadcrumbs];
newScope._tags = { ...this._tags };
newScope._extra = { ...this._extra };
Expand Down Expand Up @@ -587,6 +587,20 @@ export class Scope implements ScopeInterface {
}
}

// NOTE: By exporting this here as const & type, instead of doing `export class`,
// We can get the correct class when importing from `@sentry/core`, but the original type (from `@sentry/types`)
// This is helpful for interop, e.g. when doing `import type { Scope } from '@sentry/node';` (which re-exports this)

/**
* Holds additional event information.
*/
export const Scope = ScopeClass;

/**
* Holds additional event information.
*/
export type Scope = ScopeInterface;

function generatePropagationContext(): PropagationContext {
return {
traceId: uuid4(),
Expand Down
11 changes: 11 additions & 0 deletions packages/core/test/lib/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -985,4 +985,15 @@ describe('withIsolationScope()', () => {
done();
});
});

it('Scope type is equal to Scope from @sentry/types', () => {
// We pass the Scope _class_ here to the callback,
// Which actually is typed as using the Scope from @sentry/types
// This should not TS-error, as we export the type from core as well
const scope = withScope((scope: Scope) => {
return scope;
});

expect(scope).toBeDefined();
});
});
Loading