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
相关产品推荐
相关产品推荐

