skip to Main Content

I tried to put this script to increase the security of the system but it gave error with the typescript, in this case, I tried to put it in the app.ts

obs: this code was an attempt to convert a script in nodejs to typescript

script

import express from 'express';
import helmet from 'helmet';
import redis from 'redis';
import RateLimit from 'express-rate-limit';
import RateLimitRedis from 'rate-limit-redis';
import { resolve } from 'path';
import routes from './routes';

import ErrorHandler from './app/middlewares/errorHandler';

 class App {
  public server = express();

  constructor() {
    this.middlewares();
    this.routes();
    this.errorHandlers();
  }

  middlewares() {
    this.server.use(express.json());
    this.server.use(helmet());

    if (process.env.NODE_ENV === 'production') {
      this.server.use(
        new RateLimit({
          store: new RateLimitRedis({
            client: redis.createClient({
              host: process.env.REDIS_HOST,
              port: process.env.REDIS_PORT,
            }),
          }),
          windowMs: 1000 * 60 * 15,
          max: 100,
        }),
      );
    }
  }

region with error

new RateLimit({
      store: new RateLimitRedis({
        client: redis.createClient({
          host: process.env.REDIS_HOST,
          port: process.env.REDIS_PORT,
        }),
      }),
      windowMs: 1000 * 60 * 15,
      max: 100,
    }),

message of error

‘new’ expression, whose target lacks a construct signature, implicitly has an ‘any’ type.

2

Answers


  1. You should add type descriptions for those packages:

    npm i @types/express-rate-limit --save-dev
    npm i @types/rate-limit-redis --save-dev
    

    This goes to all other install packages as well. If you have a package that does not have a corresponding type definitions package then you should create it yourself.

    Login or Signup to reply.
  2. I believe if you change from

    new RateLimit({
        //...
    })
    

    to

    new (RateLimit as any)({
          //...
    }),
    

    Will fix the issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search