typescript - Typescript 扩展相同接口的不兼容泛型类型
问题描述
为什么处理函数的请求类型不兼容。我假设 Request 和扩展 Request 的 ApiServerRequest 是兼容的,为什么不呢?
const app = {}
type App = typeof app
const testRoute = {
method: 'GET',
path: '',
options: {
tags: ['api'],
description: '',
},
handler (request: ApiServerRequest<App>, reply: ResponseToolkit): ResponseObject {
return reply.view('landing', {
customerLandingHash: request.params.customerWalletPassId
})
}
}
打字稿编译引发错误
- 类型“(请求:ApiServerRequest)=> Lifecycle.ReturnValue”不可分配给类型“ApiServerRequestHandler”。
- 参数 'request' 和 'request' 的类型不兼容。
类型“请求”不可分配给类型“ApiServerRequest”。
- 处理程序:(请求:ApiServerRequest,回复:ResponseToolkit):ReturnValue => {
代码
import {
Server,
ServerOptions,
Lifecycle,
ServerRoute,
RouteOptions,
ResponseToolkit,
ResponseObject
} from '@hapi/hapi'
export class ApiServer<A> extends Server {
app: A = {} as A
constructor (options: ServerOptions, app: A) {
super(options)
this.app = app
return this
}
route (route: ApiServerRoute<A> | Array<ApiServerRoute<A>>): void {
super.route(route)
}
}
export interface ApiServerRequest<A> extends Request {
server: ApiServer<A>
}
export interface ApiServerRequestHandler<A> extends Lifecycle.Method {
(request: ApiServerRequest<A>, reply: ResponseToolkit, err?: Error): Lifecycle.ReturnValue
}
export interface ApiServerRoute<A> extends ServerRoute {
handler: ApiServerRequestHandler<A>,
options: ApiServerRouteOptions<A>
}
export interface ApiServerRouteOptions<A> extends RouteOptions {
tags: ['api'] | ['api', string]
description: string,
handler?: ApiServerRequestHandler<A>
}
解决方案
推荐阅读
- javascript - 我需要一些帮助来从没有父/子值的平面结构中填充我的网页上的树结构
- javascript - 使用javascript ajax根据名称对表格数据进行排序并显示
- algorithm - 嵌套循环的时间复杂度?
- python - 字典中python 3中可迭代对象的含义是什么?
- json - 如何从 Ionic 4 模板中的 JSON 字符串中获取变量?
- android - 按下状态在按钮中不起作用
- netsuite - 案例入站电子邮件捕获格式
- sql - Oracle中动态查询的索引
- sql - 基于 SQL Server 中列的交叉引用比较两个表
- spring-mvc - 带有 Spring MVC 的 Spring Webflux - 使用 doOnEach 时发出空值