import {Address} from './address.model';
export const get = async ():Promise<Address[]> => {
return await fetch(`${apiUrl}`)
.then(response => {
if (!response.ok) {
throw new Error(response.statusText)
}
return response.json() as Promise<Address[]>;
})
//--------------caling from
React.useEffect(() => {
let newArr: Address[] = get() ;
setEntities(newArr);
} , [])
//-------------抛出以下错误: 类型"Promise<Address[]>"缺少类型"Address[]"中的以下属性:长度、弹出、推送、连接等 28 个属性。 TS2740
忽略有问题的强制转换(参见as Promise<Address[]>
( 您需要等待get()
函数才能获得Address[]
类型,因此
React.useEffect(() => {
async get() => {
// do the IO
}
const newArr: Address[] = await get()
setEntities(newArr)
} , [])