简体   繁体   English

打字稿:请求类型上不存在“已解码”属性

[英]Typescript: Property 'decoded' does not exists on type Request

I have this code in javascript:我在javascript中有这段代码:

if (token) {

    // verifies secret and checks exp
    jwt.verify(token, Config.Secret, function(err, decoded) {      
      if (err) {
        return res.json({ success: false, message: 'Failed to authenticate token.' });    
      } else {
        // if everything is good, save to request for use in other routes
        req.decoded = decoded;    
        next();
      }
    });

  } else {

    // if there is no token
    // return an error
    return res.status(403).send({ 
        success: false, 
        message: 'No token provided.' 
    });

  }

Want to rewrite it to typescript but getting an error Property decoded does not exists on type Request .想要将其重写为打字稿,但收到错误Property decoded does not exists on type Request

Any ideas how to solve that?任何想法如何解决这个问题?

If you don't want to make a new class extending Request you may access request as an object like:如果您不想创建一个扩展 Request 的新类,您可以将 request 作为对象访问,例如:

req['decoded'] = decoded;

If it stills throws at you you may have to cast req as an object (typescript):如果它仍然向您抛出,您可能必须将 req 转换为对象(打字稿):

(<Object>req)['decoded'] = decoded;

You may even cast req to type any and access decoded as property (typescript):您甚至可以将 req 强制转换为输入 any 并将访问解码为属性(打字稿):

(<any>req).decoded = decoded;

You will need to use declaration merging to add type definitions for custom Request fields.您将需要使用声明合并来为自定义Request字段添加类型定义。 To do so create a .d.ts file that looks like this:为此,请创建一个.d.ts文件:

declare module Express {
  export interface Request {
    decoded: string;
    // etc.
  }
}

And add it to your compilation context so the compiler merges the type definitions with the ones in express.d.ts .并将其添加到您的编译上下文中,以便编译器将类型定义与express.d.ts的类型定义合并。

I would not merge interfaces from modules with the same name even if it's possible.即使可能,我也不会合并来自同名模块的接口。 I would rather make a new custom interface, which you can even declare in the very same file you are using it.我宁愿创建一个新的自定义界面,您甚至可以在使用它的同一个文件中声明它。 (otherwise, just export it) (否则,只需导出它)

import {Request, Response} from "express";

interface CustomRequest extends Request {
    decoded: string;
}

This is much more cleaner and moreover it's closer to OOP.这更干净,而且更接近OOP。

That error mean that the original Request type from express don't any field with that name.该错误意味着来自 express 的原始Request类型没有任何具有该名称的字段。

You have two options to solve this problem, the first one as Vadim Macagon pointed out, you can create a declaration module file and add your custom field, doing so that field will be avaliable on every Request.您有两种选择来解决这个问题,第一个如Vadim Macagon 所指出的,您可以创建一个声明模块文件并添加您的自定义字段,这样该字段将在每个请求中都可用。

declare module Express {
  export interface Request {
    decoded: string;
    // etc.
  }
}

Option two you can create a custom interface that extends the original Request and add your decoded field.选项二您可以创建一个自定义接口来扩展原始请求并添加您的解码字段。

export default interface ICustomRequest extends Request {
  token: string;
}

and then in your code you然后在你的代码中

app.post('/', function (req: ICustomRequest, res) {
  req.decoded = decoded; // No errors here
});

The first solution you're "adding" the custom field to the Request , and the second you're add your field and extending from Request.第一个解决方案是将自定义字段“添加”到Request ,第二个解决方案是添加字段并从 Request 扩展。

In your case I would use the first solution to avoid writing ICustomRequest (or whatever name you choose) every time that you have to use that decoded field from your request, the files .d.ts don't need to be imported in your code so you can just create the file and it's done.在您的情况下,我将使用第一个解决方案来避免每次必须使用请求中的decoded字段时编写 ICustomRequest(或您选择的任何名称),文件.d.ts不需要在您的代码中导入这样您就可以创建文件并完成。

my 'decoded' was just user_id string and i solved this problem like this:我的“解码”只是 user_id 字符串,我像这样解决了这个问题:

req.body.decoded = decoded;    
next();

I recently had the same issue, I followed the solution in the previous comments and this repo and I still had the same issue.我最近遇到了同样的问题,我按照之前评论和这个 repo 中的解决方案,我仍然遇到同样的问题。 After doing more digging it seems like it's a bug with ts-node.在进行更多挖掘之后,它似乎是 ts-node 的一个错误。

To solve this you need to run your server with a --files flag要解决这个问题,您需要使用 --files 标志运行您的服务器

So if you normally run your server ts-node ./src/server.ts or nodemon ./src/server.ts Change it to ts-node --files ./src/server.ts or nodemon --files ./src/server.ts因此,如果您通常运行服务器 ts-node ./src/server.ts 或 nodemon ./src/server.ts 将其更改为 ts-node --files ./src/server.ts 或 nodemon --files ./src /server.ts

After that, I was able to get rid of both the VScode errors and errors while starting the server.之后,我能够在启动服务器时摆脱 VScode 错误和错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM