Repository: tannerlinsley/react-query
File: packages/query-core/src/queryObserver.ts (TypeScript)

Code before:
      this.#currentResult = result
      this.#currentResultOptions = this.options
      this.#currentResultState = this.#currentQuery.state
    }
    return result
  }

  getCurrentResult(): QueryObserverResult<TData, TError> {
    return this.#currentResult
  }

  trackResult(
    result: QueryObserverResult<TData, TError>,
    onPropTracked?: (key: keyof QueryObserverResult) => void,
  ): QueryObserverResult<TData, TError> {
    return new Proxy(result, {
      get: (target, key) => {
        this.trackProp(key as keyof QueryObserverResult)
        onPropTracked?.(key as keyof QueryObserverResult)
        if (key === 'promise' && !this.options.experimental_prefetchInRender) {
          // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
          ;(this.#currentThenable as PendingThenable<TData>).reject?.(
            new Error(
              'experimental_prefetchInRender feature flag is not enabled',
            ),
          )
        }
        return Reflect.get(target, key)
      },
    })
  }

  trackProp(key: keyof QueryObserverResult) {
    this.#trackedProps.add(key)
  }

  getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey> {
    return this.#currentQuery
  }

  refetch({ ...options }: RefetchOptions = {}): Promise<
    QueryObserverResult<TData, TError>
  > {
    return this.fetch({
      ...options,
    })
  }

  fetchOptimistic(
    options: QueryObserverOptions<
      TQueryFnData,

Review comment (suggestion):
@KATT the type assertion + optional chaining is necessary because we might not have a `PendingThenable` here and the generic `Thenable` doesn’t have a `reject` method :/

I don’t fully understand why that is but I’m still shipping this fix 😂 

Code after:
      this.#currentResult = result
      this.#currentResultOptions = this.options
      this.#currentResultState = this.#currentQuery.state
    }
    return result
  }

  getCurrentResult(): QueryObserverResult<TData, TError> {
    return this.#currentResult
  }

  trackResult(
    result: QueryObserverResult<TData, TError>,
    onPropTracked?: (key: keyof QueryObserverResult) => void,
  ): QueryObserverResult<TData, TError> {
    return new Proxy(result, {
      get: (target, key) => {
        this.trackProp(key as keyof QueryObserverResult)
        onPropTracked?.(key as keyof QueryObserverResult)
        if (
          key === 'promise' &&
          !this.options.experimental_prefetchInRender &&
          this.#currentThenable.status === 'pending'
        ) {
          this.#currentThenable.reject(
            new Error(
              'experimental_prefetchInRender feature flag is not enabled',
            ),
          )
        }
        return Reflect.get(target, key)
      },
    })
  }

  trackProp(key: keyof QueryObserverResult) {
    this.#trackedProps.add(key)
  }

  getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey> {
    return this.#currentQuery
  }

  refetch({ ...options }: RefetchOptions = {}): Promise<
    QueryObserverResult<TData, TError>
  > {
    return this.fetch({
      ...options,
    })
  }