我正在尝试更改Express.js请求名称空间中的查询类型。我已经准备好使用自定义属性,但是如果@types中已经声明了属性,则这种方法似乎不起作用(仅适用于添加到Request中的新属性)。
这是我的类型声明:
declare namespace Express {
export interface Query { [key: string]: any; }
export interface Request {
attr1?: string, // this is working
query: any // this is not working, query is still type of Query
}
}
这是我的ts.config
"typeRoots" : [
"./src/types",
"node_modules/@types"
]
还有另一种方法,如何重新定义查询属性的类型? (或Request接口的另一个属性)
答案 0 :(得分:0)
似乎唯一的解决方案是使用Omit
import { Request as OriginalRequest } from 'express'
declare namespace Express {
export interface Request extends Omit<OriginalRequest, 'query'> {
query: any
}
}