如何扩展express.Application.get TS声明以接受返回类型?

时间:2019-02-08 00:40:52

标签: typescript-typings extend

我正在将一些对象作为助手传递给我的快速应用程序。我将它们附加到应用程序实例:

// DECREASE PLAYER LIFE AND DELETE ENEMY IF IT COLIDES WITH PLAYER
    enemyArray.forEach(function(enemy) {
    if (collides(player, enemy)) {
        player.life += -10;
        enemy.delete();
    }
    });

      // ANIMATION LOOP

      function animate(currentTime) {
        const animation = requestAnimationFrame(animate);
        c.clearRect(0, 0, canvas.width, canvas.height);



        // PLAYER LIFE
        // RED REC
        c.beginPath();
        c.rect(20, 20, 150, 30);
        c.fillStyle = "red";
        c.fillRect(10, 30, 200, 20);
        // YELLOW REC
        c.beginPath();
        c.rect(20, 20, 150, 30);
        c.fillStyle = "yellow";
        c.fillRect(10, 30, player.life, 20);


        // END GAME

       //I need some delay here so that fillRect has time to decrease the yellow bar to 0;
        if (player.life <= 0) {
          console.log("You Lose");
          music.pause();
          paused = true;
          cancelAnimationFrame(animation);
        }
      }
      animate();

具有让我在处理某些请求时可以访问特定帮助器的功能:

app.set('logger', logger); // logger has Logger type

我要实现的是让const logger = req.app.get('logger') as Logger; // or const logger: Logger = req.app.get('logger'); 对象公开通用的Application.get方法。我想开始使用的代码如下:

req.app

我在项目根目录下的内容下方创建了const logger = req.app.get<Logger>('logger'); 文件:

@types/express.d.ts

我的import * as express from 'express'; declare module 'express' { export interface Application { get<T>(name: string): T; } } 涵盖了我将键入扩展名放在的目录:

tsconfig.json

即使我声明键入扩展名TS仍然仅看到"typeRoots": ["node_modules/@types", "@types"], 定义:

@types/express

我希望它也能涵盖我创建的定义。

0 个答案:

没有答案