Repository: babel/babel
File: packages/babel-helpers/src/helpers/asyncIterator.ts (TypeScript)

Code before:
        // Itentionally casting "arguments" to an array for the type of func.apply
        this.n.apply(this.s, arguments as any as [] | [undefined]),
      );
    },
    return: function (value) {
      var ret = this.s.return;
      if (ret === undefined) {
        return Promise.resolve<IteratorReturnResult<TReturn>>({
          // "TReturn | PromiseLike<TReturn>" should have been unwrapped by Awaited<T>,
          // but TypeScript choked, let's just casting it away
          value: value as TReturn,
          done: true,
        });
      }
      return AsyncFromSyncIteratorContinuation<T, TReturn>(
        ret.apply(
          this.s,
          // Use "arguments" here for better compatibility and smaller bundle size
          // Itentionally casting "arguments" to an array for the type of func.apply
          arguments as any as [] | [TReturn | PromiseLike<TReturn>],
        ),
      );
    },
    throw: function (maybeError?: any) {
      var thr = this.s.return;
      if (thr === undefined) return Promise.reject(maybeError as Error);
      return AsyncFromSyncIteratorContinuation<T, TReturn>(
        // Use "arguments" here for better compatibility and smaller bundle size
        // Itentionally casting "arguments" to an array for the type of func.apply
        thr.apply(this.s, arguments as any as [any]),
      );
    },
  } satisfies AsyncFromSyncIterator<T, TReturn, TNext>;

  function AsyncFromSyncIteratorContinuation<T, TReturn>(r: any) {
    // This step is _before_ calling AsyncFromSyncIteratorContinuation in the spec.
    if (Object(r) !== r) {
      return Promise.reject(new TypeError(r + " is not an object."));
    }

    var done = r.done;
    return Promise.resolve(r.value).then<IteratorResult<T, TReturn>>(
      function (value) {
        return { value: value, done: done };
      },
    );
  }

  return new AsyncFromSyncIterator(s);
}


Review comment (bug):
Given that `maybeError` here could be also something that is _not_ an error, I would prefer to use an `eslint-disable-next-line` comment rather than a "wrong" cast.

Code after:
        // Itentionally casting "arguments" to an array for the type of func.apply
        this.n.apply(this.s, arguments as any as [] | [undefined]),
      );
    },
    return: function (value) {
      var ret = this.s.return;
      if (ret === undefined) {
        return Promise.resolve<IteratorReturnResult<TReturn>>({
          // "TReturn | PromiseLike<TReturn>" should have been unwrapped by Awaited<T>,
          // but TypeScript choked, let's just casting it away
          value: value as TReturn,
          done: true,
        });
      }
      return AsyncFromSyncIteratorContinuation<T, TReturn>(
        ret.apply(
          this.s,
          // Use "arguments" here for better compatibility and smaller bundle size
          // Itentionally casting "arguments" to an array for the type of func.apply
          arguments as any as [] | [TReturn | PromiseLike<TReturn>],
        ),
      );
    },
    throw: function (maybeError?: any) {
      var thr = this.s.return;
      if (thr === undefined) {
        // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
        return Promise.reject(maybeError);
      }
      return AsyncFromSyncIteratorContinuation<T, TReturn>(
        // Use "arguments" here for better compatibility and smaller bundle size
        // Itentionally casting "arguments" to an array for the type of func.apply
        thr.apply(this.s, arguments as any as [any]),
      );
    },
  } satisfies AsyncFromSyncIterator<T, TReturn, TNext>;

  function AsyncFromSyncIteratorContinuation<T, TReturn>(r: any) {
    // This step is _before_ calling AsyncFromSyncIteratorContinuation in the spec.
    if (Object(r) !== r) {
      return Promise.reject(new TypeError(r + " is not an object."));
    }

    var done = r.done;
    return Promise.resolve(r.value).then<IteratorResult<T, TReturn>>(
      function (value) {
        return { value: value, done: done };
      },
    );
  }