【问题标题】:Extend Polka Request object using Typescript使用 Typescript 扩展 Polka Request 对象
【发布时间】:2021-06-11 20:24:09
【问题描述】:

首先,我知道这个话题看起来很像 this other topic 谈论 使用 Typescript 扩展 Express Request 对象

基本上,我尝试做同样的事情,但这次使用Polka

有人能做到吗?

我走的路径类似于this

在项目根杆我创建了这个文件夹结构:

app/
├─ src/
│  ├─ @types/
│  │  ├─ polka/
│  │  │  ├─ index.d.ts

我在index.d.ts中添加了这个

import * as polka from "polka";

declare global {
  namespace polka {
    interface Request {
      foo: string;
    }
  }
}

我还通过添加以下内容更新了我的tsconfig.json

"typeRoots": [ "@types" ]

我为请求分配值的中间件如下所示

import type { Middleware } from "polka";

export const dummyMiddleware: Middleware = (req, res, next) => {

  req.foo = "hello";
  next();
};

这样做我有这个打字稿错误:

Property 'foo' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs,

当我查看 Polka Middleware definition 时,我看到这种类型是通用的。

我试图做这样的事情

import type { Middleware } from "polka";

export const dummyMiddleware: Middleware<{foo : string}> = (req, res, next) => {

  req.foo = "hello";
  next();
};

但是错误信息只变成了这个

Property 'foo' does not exist on type 'Request<{ foo: string; }, any, any, ParsedQs, Record<string, any>>'.ts(2339)

那么,问题是,声明合并是实现这一目标的最佳方式吗?如果是,您是否有适当的方法来实现这一目标?

一点上下文,这个中间件将用于seeding Sapper session data

版本:

  • “打字稿”:“^4.0.3”
  • “波尔卡”:“下一个”
  • “@types/polka”:“^0.5.2”,

完整的 TypeScript 配置:

{
        "extends": "@tsconfig/svelte/tsconfig.json",
        "compilerOptions": {
         "module": "esnext",
            "lib": ["DOM", "ES2017", "WebWorker", "ESNext"],
            "strict": true
        },
        "include": ["src/**/*", "src/node_modules/**/*"],
        "exclude": ["node_modules/*", "__sapper__/*", "static/*"],
        "typeRoots": [ "@types" ]
    }

免责声明:

我是波尔卡和打字的新手,所以我错过了一些明显的东西并非不可能

【问题讨论】:

    标签: typescript sapper polka


    【解决方案1】:

    我首先将IncomingMessage 转换为Request

    import { IncomingMessage, ServerResponse } from 'node:http';
    import polka, { Next, Request } from 'polka';
    
    function one(req:IncomingMessage, res:ServerResponse, next:Next) {
      (req as Request).foo = 'world';
      next();
    }
    
    function two(req:IncomingMessage, res:ServerResponse, next:Next) {
      //req.foo = '...needs better demo ?';
      next();
    }
    
    const app = polka();
    app.use(one, two);
    app.get('/users/:id', (req:IncomingMessage, res) => {
      console.log(`~> Hello, ${(req as Request).foo}`);
      res.end(`User: ${(req as Request).params.id}`);
    });
    app.listen(3000, (err:any) => {
      if (err) throw err;
      console.log(`> Running on localhost:3000`);
    });
    

    然后在index.d.ts 试试这个:

    declare global{
      module 'polka' {
        interface Request {
          foo: string;
        }
      }
    }
    

    不是很好,但可以使用。

    【讨论】:

    • 到目前为止,您提出的解决方案对我来说效果很好!非常感谢你。我只是希望它不会像the isssue with express 那样成为一个“移动”问题据我所知,当你说“不好”时,你能告诉我更多关于为什么你不认为它很好吗?我也有同样的感觉,但我说不出为什么。但到目前为止一切顺利!
    猜你喜欢
    • 2015-11-05
    • 2016-09-19
    • 2021-05-30
    • 2021-10-23
    • 2023-02-25
    • 2016-05-06
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    相关资源
    最近更新 更多