FlowType包装获取而不会丢失默认types的注释

我想要做的就是包装fetch函数来处理一些类似调用.json()fetch样板文件。

但问题是,如果我包装fetch函数,在一个函数与我自己的注释,我失去了很多的types安全,因为我的types将永远不会像默认情况下与stream来的具体。

所以我试图利用存在types(*)typeof来使Flow保持默认的注释

 // Imported as f because importing as "fetch" // overwrites all of Flow's default type info about fetch import f from 'node-fetch' export const fetchJSON: typeof fetch = (url: *, opts: *): * => f(url, opts).then(r => r.json()) export const post: typeof fetchJSON = (url: *, opts: *): * => fetchJSON(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, ...opts }) 

不幸的是,这给了我一些types的错误,没有多大意义

  10: export const post: typeof fetchJSON = (url: *, opts: *): * => fetchJSON(url, { ^ object literal 11: method: 'POST', ^^^^^^ string. This type is incompatible with 824: method?: ?MethodType; ^^^^^^^^^^^ null. See lib: /private/tmp/flow/flowlib_22594590/bom.js:824 x.js:10 10: export const post: typeof fetchJSON = (url: *, opts: *): * => fetchJSON(url, { ^ object literal 12: headers: { 'Content-Type': 'application/json' }, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ object literal. This type is incompatible with 822: headers?: ?HeadersInit; ^^^^^^^^^^^^ null. See lib: /private/tmp/flow/flowlib_22594590/bom.js:822 x.js:10 10: export const post: typeof fetchJSON = (url: *, opts: *): * => fetchJSON(url, { ^ object literal 12: headers: { 'Content-Type': 'application/json' }, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ property `Content-Type`. Property not found in 822: headers?: ?HeadersInit; ^^^^^^^^^^^ Headers. See lib: /private/tmp/flow/flowlib_22594590/bom.js:822 Found 3 errors 

有什么办法可以做这个工作吗?

我能够用更小的一个更大的问题子集重现这一点,我想我现在明白了这个问题,因为它与fetch本身是完全无关的。

 // @flow const opts: RequestOptions = {} const test = { method: 'POST', headers: { 'Content-Type': 'application/json' }, ...opts } 

上面的原因导致类似的错误,这是因为将opts传入对象可能会将'POST'重写为null ,这是不允许的,因为它具有stringtypes。 解决scheme是传播到一个新的对象,而不是现有的。 所以

 const test = { ...{ method: 'POST', headers: { 'Content-Type': 'application/json' } }, ...opts } 

将工作。