You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript报Cannot find name 'async' 接口定义异步高阶函数问题

修复方案

错误根源

  • async 是函数实现时使用的语法关键字,不属于TypeScript类型定义的合法语法,不能出现在接口的方法类型签名中
  • 异步函数的类型特征仅通过返回值为Promise即可体现,不需要额外标记async
  • 你原有接口中checkRoles的返回值声明语法也存在错误,不能用=>连接方法参数和返回值

正确的接口定义

你可以任选以下两种写法之一,均可以和你现有checkRoles实现完全兼容,无需修改实现代码:

写法1:方法签名格式

interface IUsers {
  checkRoles(requiredRoles: string[]): (_req: Request, res: Response, next: NextFunction) => Promise<void>;
  checkIsVerified(_req: Request, res: Response, next: NextFunction): Promise<void>;
  isAuthenticated(_req: Request, res: Response, next: NextFunction): Promise<void>;
}

写法2:函数类型赋值格式

interface IUsers {
  checkRoles: (requiredRoles: string[]) => (_req: Request, res: Response, next: NextFunction) => Promise<void>;
  checkIsVerified: (_req: Request, res: Response, next: NextFunction) => Promise<void>;
  isAuthenticated: (_req: Request, res: Response, next: NextFunction) => Promise<void>;
}

内容的提问来源于stack exchange,提问作者tomas jonauskas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 02:12:14