如何在打字稿中使用获取
我正在使用 window> window.fetch..fetch 在Typescript中直接进入我的自定义类型:
我通过将承诺结果扔到中间的“任何”变量来解决这一问题。
做到这一点的正确方法是什么?
import { Actor } from './models/actor';
fetch(`http://swapi.co/api/people/1/`)
.then(res => res.json())
.then(res => {
// this is not allowed
// let a:Actor = <Actor>res;
// I use an intermediate variable a to get around this...
let a:any = res;
let b:Actor = <Actor>a;
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不幸的是
,不幸的是,
fet> fetch
也不是响应
它返回接受通用输入,因此我们需要进行某种类型的铸造(将其视为为t
以下)。但是,通常会在您自己的项目中获取以提供任何通用标题,基本URL等,以便您可以在应用程序中允许通用输入。
包装摘要的示例,并允许应用程序定义每个API响应的形状。
Playground
Previous versions
Note :以前的版本在撰写本文时使用了承诺,在浏览器中通常会发现/异步。从学习的角度来看,它们仍然很有用,因此保留它们。
承诺版本
自一段时间以来编写此答案以来发生了一些更改。如评论中所述,
wenders.json&lt; t&gt;
不再有效。不确定,找不到将其删除的地方。对于以后的发行版,您可以执行:
旧答案
以下几个示例,从基本到请求和/或错误处理后添加转换:
基本:
数据转换:
通常您可能需要对数据进行一些调整,然后例如,对于消费者而言,取消顶级数据属性。这是直接的:
错误处理:
我认为您不应该直接在此服务中直接捕获错误,而只是允许它起泡,但是如果需要,您可以执行以下操作:
Await version
Unfortunately, neither
fetch
, nor theresponse
it returns accept generic input, so we need to do some type casting (seen asas T
below).However, it is common to wrap fetch in your own project to provide any common headers, a base url, etc, and so you can allow generic input within your application.
Example of wrapping fetch, and allowing the application to define the shape of each api response.
Playground
Previous versions
Note: The previous versions used promises at the time of writing, prior to await/async being commonly found in the browser. They are still useful from a learning perspective, and so they are kept.
Promise version
There has been some changes since writing this answer a while ago. As mentioned in the comments,
response.json<T>
is no longer valid. Not sure, couldn't find where it was removed.For later releases, you can do:
Old Answer
A few examples follow, going from basic through to adding transformations after the request and/or error handling:
Basic:
Data transformations:
Often you may need to do some tweaks to the data before its passed to the consumer, for example, unwrapping a top level data attribute. This is straight forward:
Error handling:
I'd argue that you shouldn't be directly error catching directly within this service, instead, just allowing it to bubble, but if you need to, you can do the following:
实际上,只要传递的类型兼容,就几乎在打字稿中的任何地方,都将值传递给具有指定类型的函数。
话虽如此,以下工作...
我想将所有HTTP调用包装在可重复使用的类中 - 这意味着我需要某种方法让客户以所需的形式处理响应。为了支持这一点,我接受回调lambda作为我的包装方法的参数。 Lambda声明接受此处所示的任何类型...
但是在使用中,呼叫者可以通过指定所需返回类型的lambda。我像这样修改了上面的代码...
以便客户可以用像...的回调调用它。
Actually, pretty much anywhere in typescript, passing a value to a function with a specified type will work as desired as long as the type being passed is compatible.
That being said, the following works...
I wanted to wrap all of my http calls in a reusable class - which means I needed some way for the client to process the response in its desired form. To support this, I accept a callback lambda as a parameter to my wrapper method. The lambda declaration accepts an any type as shown here...
But in use the caller can pass a lambda that specifies the desired return type. I modified my code from above like this...
So that a client can call it with a callback like...
如果您看一下您将看到身体的定义
,这意味着您可以使用仿制药来实现所需的目标。我没有测试此代码,但看起来像这样:
If you take a look at @types/node-fetch you will see the body definition
That means that you could use generics in order to achieve what you want. I didn't test this code, but it would looks something like this:
这是专门为
发布
请求而编写的。这就是为什么它具有“变量”参数。如果“获取”请求相同的代码将起作用,则可以选择可选This is specifically written for
POST
request. That is why it has "variables" parameter. In case of "GET" request same code will work, vriables can be optional is handled对于此特定的用例:
“从远程资源获取数据,我们没有控制权,并且想要在当前应用程序中注入过滤器
”
https://www.npmjs.com/package/package/zod
以下时尚:
For this particular use-case:
"Fetching data from a remote resource, we do not have control and want to validate filter before injecting in our current application"
I feel recommending zod npm package
https://www.npmjs.com/package/zod
with the following fashion:
新解决方案(2024-12)
当我自己在工作中面临问题时,我对这个话题感兴趣的 。最受欢迎的答案对我来说是一个很大的否定,因为:
wenders.ok
是true
在无论如何,我需要键入所有可能的身体:例如,我的API在400个不良请求响应中返回一个有意义的身体。
没有什么可以解决 n-types-for-n-status-codes 问题,所以我做了自己的:npm软件包 dr-fetch 通过使用链语法累积类型来解决此问题。
首先,创建Fetcher对象,该对象允许自定义的身体解析器和自定义的数据提取功能。前者是用
content-type
的响应来处理不常见的响应;后者是进行前/提取工作:现在我们在需要的地方导入此fetcher对象,为URL键入并获取:
看到吗?这是非常清楚,简洁的代码,不需要
try..catch
非OK响应。停止疯狂:拒绝进行非OK回应。Newer Solution Available (2024-12)
I got interested in this topic when I was faced with the problem myself at work. The most accepted answer is a big NO to me because:
response.ok
istrue
Anyway, I needed to type all possible bodies: For example, my API returns a meaningful body on 400 BAD REQUEST responses.
There was nothing out there that solves the N-types-for-N-status-codes problem, so I made my own: The NPM package dr-fetch resolves this by accumulating types using chain syntax.
First, create the fetcher object, which allows for custom body parsers and customized data-fetching functions. The former is to handle responses with
content-type
's that are uncommon; the latter is to do pre/post fetch work:Now we import this fetcher object where needed, type it for the URL, and fetch:
See? This is very clear, concise code that doesn't require
try..catch
for non-OK responses. Stop the madness: Say no to throwing on non-OK responses.