javascript - 打字稿 - 面临的问题属性不能分配给基本类型中的相同属性
问题描述
我在打字稿中遇到问题。我创建了两个接口,一个用于类,另一个用于在类中定义的函数。这是我的 Schedule 类的接口
interface scheduleClass {
getScheduleLists(): showListsInterface
}
这是我的显示列表功能的界面。
interface showListsInterface {
status: string;
statusCode: number;
message: string;
data: Promise<Schedule[]>
}
这是时间表类
export class scheduleServices implements scheduleClass{
async getScheduleLists(){
try {
const schedules = await Schedule.findAll();
return {
status:"success",
statusCode:200,
message:"",
data:schedules
}
} catch (error) {
return {
status:"error",
statusCode:500,
message:error.message,
data:[]
}
}
}
有人可以解释我做错了什么吗?
解决方案
您的返回类型不正确。async
函数总是返回 a Promise
,但你的类型是一个对象,其中有一个承诺。它需要是对整个事情的承诺。
我会在没有承诺的情况下制作类型:
interface showListsInterface {
status: string;
statusCode: number;
message: string;
data: Schedule[];
}
那么函数的返回类型是Promise<showListsInterface>
.
我本来想说“或更改函数以返回带有承诺的对象”,但我看到对象的其他部分依赖于知道异步部分是否有效,因此在这种特定情况下这不是一个选项。
推荐阅读
- mysql - 使用 SET 语法将输入作为变量传递时,MySQL 存储过程不返回输出
- node.js - “发送后无法设置标题” Passport js on successredirect
- python - 如何从多个子进程登录到同一个文件
- html - 如何不对 :before 中添加的内容应用悬停效果?css
- git - Git 子模块获取不正确的提交哈希
- coq - 如何证明 le 的所有证明相等?
- snowflake-cloud-data-platform - 在 SELECT 中设置变量
- javascript - 如何使用 Svelte Routify 创建路由链接
- pixi.js - pixi.js 我如何获得绘制计数
- python - Pandas - 检查数据框是否在任何列中具有负值