相关疑难解决方法(0)

如何在打字稿中使用fetch

我在Typescript中使用window.fetch,但我无法将响应直接转换为我的自定义类型:

我通过将Promise结果转换为中间的'any'变量来解决这个问题.

这样做的正确方法是什么?

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;
      })
Run Code Online (Sandbox Code Playgroud)

promise typescript

33
推荐指数
3
解决办法
5万
查看次数

标签 统计

promise ×1

typescript ×1