如何解决 typescript 中 type 'promise' is not assignable to type 错误-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 编程语言 > >

如何解决 typescript 中 type 'promise' is not assignable to type 错误

作者:迹忆客 最近更新:2023/01/31 浏览次数:

当我们尝试将具有 promise 类型的值分配给具有不兼容类型的值时,会发生“type 'promise' is not assignable to type”错误。 要解决错误,需要在赋值之前解决 promise 并使两个兼容类型的值。

下面是产生上述错误的一个示例。

// 👇️ function example(): promise
async function example() {
  const result = await promise.resolve('hello world');
  return result;
}
// ⛔️ error: type 'promise' is
// not assignable to type 'string'.ts(2322)
const str: string = example();

typescript 中 type 'promise' is not assignable to type 错误

该函数被标记为异步,所有异步函数都返回一个 promise。 上面示例中的函数的返回类型为 promise

typescript 告诉我们不能将 promise 类型的值赋给字符串类型的变量 str——赋值两端的类型不兼容。

要解决错误,请在分配之前解决 promise

async function example() {
  const result = await promise.resolve('hello world');
  return result;
}
example().then((value) => {
  const str: string = value;
  console.log(str); // 👉️ "hello world"
});

在将值分配给 str 变量之前,我们使用 .then() 方法来解决承诺。

如果我们尝试在异步函数中解析 promise,请使用 await 语法。

错误的一个常见原因是我们忘记等待 promise

async function example() {
  // 👇️ forgot to use await
  const result = promise.resolve('hello world');
  // ⛔️ error: type 'promise' is
  // not assignable to type 'string'.ts(2322)
  const greeting: string = result;
  return greeting;
}

函数中的结果变量具有 promise 类型,我们试图将其分配给需要字符串的变量。

要解决该错误,需要在赋值之前使用 await 关键字来解析 promise

async function example() {
  // 👇️ const result: string
  const result = await promise.resolve('hello world');
  const greeting: string = result;
  return greeting;
}

我们使用了 await 关键字,现在 result 变量存储了一个字符串。

现在 greetingresult 变量具有兼容的类型,因此可以在不出现类型检查错误的情况下进行赋值。

这就是错误的原因——我们试图将 promise 类型的值分配给具有不同类型的值。

要解决这个错误,我们必须确保赋值语句左右两侧的值具有兼容的类型。

当使用 .then() 语法来解析 promise 时,请注意它是异步的,并且只能在传递给 then() 方法的回调函数中访问已解决的值。

async function example() {
  const result = await promise.resolve({
    name: 'tom',
    country: 'chile',
  });
  return result;
}
type person = {
  name: string;
  country: string;
};
example().then((value) => {
  const person: person = value;
  console.log(person); // 👉️ {name: 'tom', country: 'chile'}
});
// 👇️ code here runs before example().then() has finished

总结

当我们尝试将具有 promise 类型的值分配给具有不兼容类型的值时,会发生“type 'promise' is not assignable to type”错误。 要解决错误,需要在赋值之前解决 promise 并使两个兼容类型的值。

上一篇:

下一篇:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

发布时间:2023/03/19 浏览次数:254 分类:

本教程指南通过特定的实现和编码示例深入了解了 typescript 中 declare 关键字的用途。

发布时间:2023/03/19 浏览次数:962 分类:

本篇文章演示了类的 get 和 set 属性以及如何在 typescript 中实现它。

在 typescript 中格式化日期和时间

发布时间:2023/03/19 浏览次数:269 分类:

本教程介绍内置对象 date() 并讨论在 typescript 中获取、设置和格式化日期和时间的各种方法。

在 typescript 中返回一个 promise

发布时间:2023/03/19 浏览次数:586 分类:

本教程讨论如何在 typescript 中返回正确的 promise。这将提供 typescript 中 returns promise 的完整编码示例,并完整演示每个步骤。

在 typescript 中定义函数回调的类型

发布时间:2023/03/19 浏览次数:1445 分类:

本教程说明了在 typescript 中为函数回调定义类型的ag捕鱼王app官网的解决方案。为了程序员的方便和方便,实施了不同的编码实践指南。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图