Skip to content

startSpan/startSpanManual wraps promise-like objects in a way that’s not reflected by the TypeScript type #19242

@andersk

Description

@andersk

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/browser

SDK Version

10.38.0

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

This code is accepted by TypeScript, but throws Uncaught TypeError: jqXHR.abort is not a function at runtime.

import * as Sentry from "@sentry/core";
import $ from "jquery";

const jqXHR = Sentry.startSpan({ name: "test" }, () =>
  $.ajax("https://http.codes/200"),
);
jqXHR.abort();

$.ajax() returns a jqXHR object that’s a superset of both Promise and XMLHTTPRequest. However, Sentry’s maybeHandlePromiseRejection(value) wraps the Promise part using .then(…) without preserving the XMLHTTPRequest part of the interface. So its type annotation claiming that it returns the same type as value is incorrect (masked by // @ts-expect-error).

(This leads to a bug in Zulip.)

Steps to Reproduce

Open this HTML page in a browser and look at the console:

<!doctype html>
<title>Test</title>
<script type="module">
  import * as Sentry from "https://esm.sh/@sentry/core";
  import $ from "https://esm.sh/jquery";

  const jqXHR = Sentry.startSpan({ name: "test" }, () =>
    $.ajax("https://http.codes/200"),
  );
  jqXHR.abort();
</script>

Expected Result

Either

  1. this should run successfully at runtime (perhaps maybeHandlePromiseRejection could allow the wrapped promise to float and return the original promise-like object instead); or
  2. this should not be accepted by TypeScript (perhaps using a return type like value extends PromiseLike<infer T> ? PromiseLike<Awaited<T>> : T).

Actual Result

Uncaught TypeError: jqXHR.abort is not a function

Additional Context

No response

Priority

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.

Metadata

Metadata

Assignees

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions