📜  在 ts 中获取 - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:32.644000             🧑  作者: Mango

代码示例1
// Implementation code where T is the returned data shape
function api(url: string): Promise {
  return fetch(url)
    .then(response => {
      if (!response.ok) {
        throw new Error(response.statusText)
      }
      return response.json()
    })

}

// Consumer
api<{ title: string; message: string }>('v1/posts/1')
  .then(({ title, message }) => {
    console.log(title, message)
  })
  .catch(error => {
    /* show error message */
  })