如何解决 typescript 中 type 'promise' is not assignable to type 错误
当我们尝试将具有 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();
该函数被标记为异步,所有异步函数都返回一个 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 变量存储了一个字符串。
现在 greeting 和 result 变量具有兼容的类型,因此可以在不出现类型检查错误的情况下进行赋值。
这就是错误的原因——我们试图将 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 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在 angularjs 中设置 select from typescript 的默认选项值
发布时间:2023/04/14 浏览次数:132 分类:angular
-
本教程提供了在 angularjs 中从 typescript 中设置 html 标记选择的默认选项的解释性ag捕鱼王app官网的解决方案。
在 angular 中使用 typescript 的 getelementbyid 替换
发布时间:2023/04/14 浏览次数:259 分类:angular
-
本教程指南提供了有关使用 typescript 在 angular 中替换 document.getelementbyid 的简要说明。这也提供了在 angular 中 getelementbyid 的最佳方法。
在 typescript 中使用 try..catch..finally 处理异常
发布时间:2023/03/19 浏览次数:385 分类:
-
本文详细介绍了如何在 typescript 中使用 try..catch..finally 进行异常处理,并附有示例。
发布时间: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官网的解决方案。为了程序员的方便和方便,实施了不同的编码实践指南。