Typescript 财产';id';不存在于类型';参数';
我正在使用Typescript构建一个expressapi。我使用id参数设置了路由,如下所示:Typescript 财产';id';不存在于类型';参数';,typescript,express,Typescript,Express,我正在使用Typescript构建一个expressapi。我使用id参数设置了路由,如下所示: router.route('/cheeses/:id') .get(controller.showRoute) 我的控制器如下所示: export function showRoute(req: Request, res: Response, next: NextFunction): void { Cheese.findById(req.params.id) .then((chee
router.route('/cheeses/:id')
.get(controller.showRoute)
我的控制器如下所示:
export function showRoute(req: Request, res: Response, next: NextFunction): void {
Cheese.findById(req.params.id)
.then((cheese: ICheeseModel) => res.json(cheese))
.catch(next)
}
declare namespace Express {
export interface Request {
params: any;
}
}
但是,我得到以下错误:
lib/controllers/cheeses.ts:11:30 - error TS2339: Property 'id' does not exist on type 'Params'.
Property 'id' does not exist on type 'string[]'.
11 Cheese.findById(req.params.id)
~~
从我读到的req.params
应该是any
类型,但它似乎被设置为字符串数组
这是我的tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"pretty": true,
"sourceMap": true,
"target": "es6",
"outDir": "./dist",
"baseUrl": "./lib"
},
"include": [
"lib/**/*.ts"
],
"exclude": [
"node_modules"
]
}
和my package.json:
{
"name": "typescript-express-api",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"build": "tsc",
"dev": "nodemon",
"start": "node ./dist/server"
},
"dependencies": {
"@types/express": "^4.17.0",
"@types/mongoose": "^5.5.12",
"@types/node": "^12.7.2",
"@types/webrtc": "^0.0.25",
"express": "^4.17.1",
"mongoose": "^5.6.9",
"ts-node": "^8.3.0",
"typescript": "^3.5.3"
}
}
我还使用nodemon在开发过程中观察变化。这是我的nodemon.json:
{
"watch": ["lib"],
"ext": "ts",
"exec": "ts-node ./lib/app.ts"
}
我尝试了以下方法:,类似于:
export function showRoute(req: Request, res: Response, next: NextFunction): void {
Cheese.findById(req.params.id)
.then((cheese: ICheeseModel) => res.json(cheese))
.catch(next)
}
declare namespace Express {
export interface Request {
params: any;
}
}
但似乎没有效果。这里的问题也完全一样。相同的ts节点和类型脚本版本。问题首先发生在12小时前的一个代码库上,该代码库在去年没有更改。啊,好吧,那么这可能是某个东西的新版本?我将尝试回滚一些依赖项…好的,那么回滚到@types/express 4.0.32可以修复这个问题。非常令人惊讶,因为还有13个比这更新的版本。他们将
params
的类型从any
更改为string[]
,并在几天后更改为其他类型。因此,它目前似乎是一个移动的目标。返回到一个版本,params
仍然是any
类型。这里的问题与此完全相同。相同的ts节点和类型脚本版本。问题首先发生在12小时前的一个代码库上,该代码库在去年没有更改。啊,好吧,那么这可能是某个东西的新版本?我将尝试回滚一些依赖项…好的,那么回滚到@types/express 4.0.32可以修复这个问题。非常令人惊讶,因为还有13个比这更新的版本。他们将params
的类型从any
更改为string[]
,并在几天后更改为其他类型。因此,它目前似乎是一个移动的目标。回滚到一个版本,其中params
仍然是any
类型。