Skip to content

Commit 5efdabe

Browse files
authored
fix(utils): Remove Element dom type (#5675)
This PR removes the use of the `Element` DOM type from `@sentry/utils`, introduced with #5594. This breaks `@sentry/node` users that use strict TS configs. We have to cast the type to `any` to make sure it works in different environments, but we supply a generic argument so that users can specify what type of `Element` will be returned from `querySelector`.
1 parent 53915b0 commit 5efdabe

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

packages/tracing/src/browser/browsertracing.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ export function extractTraceDataFromMetaTags(): Partial<TransactionContext> | un
294294

295295
/** Returns the value of a meta tag */
296296
export function getMetaContent(metaName: string): string | null {
297+
// Can't specify generic to `getDomElement` because tracing can be used
298+
// in a variety of environments, have to disable `no-unsafe-member-access`
299+
// as a result.
297300
const metaTag = getDomElement(`meta[name=${metaName}]`);
301+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
298302
return metaTag ? metaTag.getAttribute('content') : null;
299303
}

packages/utils/src/browser.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,21 @@ export function getLocationHref(): string {
129129
* This wrapper will first check for the existance of the function before
130130
* actually calling it so that we don't have to take care of this check,
131131
* every time we want to access the DOM.
132-
* Reason: DOM/querySelector is not available in all environments
132+
*
133+
* Reason: DOM/querySelector is not available in all environments.
134+
*
135+
* We have to cast to any because utils can be consumed by a variety of environments,
136+
* and we don't want to break TS users. If you know what element will be selected by
137+
* `document.querySelector`, specify it as part of the generic call. For example,
138+
* `const element = getDomElement<Element>('selector');`
133139
*
134140
* @param selector the selector string passed on to document.querySelector
135141
*/
136-
export function getDomElement(selector: string): Element | null {
142+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
143+
export function getDomElement<E = any>(selector: string): E | null {
137144
const global = getGlobalObject<Window>();
138145
if (global.document && global.document.querySelector) {
139-
return global.document.querySelector(selector);
146+
return global.document.querySelector(selector) as unknown as E;
140147
}
141148
return null;
142149
}

0 commit comments

Comments
 (0)