te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Why is Typescript expecting fetch()'s `body` property to be ReadableStream<Uint8Array>? - Sta
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is Typescript expecting fetch()'s `body` property to be ReadableStream<Uint8Array>? - Sta

programmeradmin3浏览0评论
  const request: RequestInfo = {
    method,
    cache,
    redirect,
    headers: {} as Headers,
    body: null as string | null,
  };

  ...

  fetch(url, request);

With the above, I get:

Type 'string | null' is not assignable to type 'ReadableStream<Uint8Array> | null'.
  Type 'string' is not assignable to type 'ReadableStream<Uint8Array> | null'.

In TS's type declarations, there's:

interface RequestInit {
    /**
     * A BodyInit object or null to set request's body.
     */
    body?: BodyInit | null;
}

type BodyInit = Blob | BufferSource | FormData | URLSearchParams | ReadableStream<Uint8Array> | string;

Why is it expecting ReadableStream<Uint8Array>?

  const request: RequestInfo = {
    method,
    cache,
    redirect,
    headers: {} as Headers,
    body: null as string | null,
  };

  ...

  fetch(url, request);

With the above, I get:

Type 'string | null' is not assignable to type 'ReadableStream<Uint8Array> | null'.
  Type 'string' is not assignable to type 'ReadableStream<Uint8Array> | null'.

In TS's type declarations, there's:

interface RequestInit {
    /**
     * A BodyInit object or null to set request's body.
     */
    body?: BodyInit | null;
}

type BodyInit = Blob | BufferSource | FormData | URLSearchParams | ReadableStream<Uint8Array> | string;

Why is it expecting ReadableStream<Uint8Array>?

Share Improve this question asked Aug 19, 2020 at 23:51 Leo JiangLeo Jiang 26.1k58 gold badges176 silver badges327 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

Not sure to pletely understand your actual question, but let's answer both possibilities.

The RequestInfo type is the one used for a Request instance, e.g

const request: RequestInfo = new Request(url, requestInit);

The object you are creating is actually the requestInit second parameter of fetch, and for this you need to use the RequestInit type.

const requestInit: RequestInit = {
  method,
  cache,
  redirect,
  headers: {} as Headers,
  body: null as string | null,
};
...
fetch(url, requestInit);

Now if you were wondering why the RequestInit's BodyInit member says it can be a ReadablStream<Uint8Array>, that's because per specs it can, even though no browser supports it yet and the specs are not really bullet-proof yet.

The definitions you show are not quite right, you show RequestInit, not RequestInfo. What I have (and what I could see on microsoft pages with TS definitions) is this

type RequestInfo = Request | string
interface Request extends Body {
  // No definition for `body` here
  // ...
}
interface Body {
  readonly body: ReadableStream<Uint8Array> | null
  // ...
}

So it's clear, why the piler plains. But it's not clear to me, why the definition is like so, because I'm sure it's possible to pass string as the body of a request. I think you will have to cast it to readable stream:

body: 'string' as unknown as ReadableStream<Uint8Array>

Or just cast it to any, because this type cast is pretty long. I don't think casting to any will cause any confusion, errors or problems here

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论