我在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)