Skip to content
Advertisement

An interface can only extend an identifier/qualified-name with optional type arguments. ts(2499)

I am trying to create a new extended interface for express.RequestHandler but this error seems to come up. I cannot understand why.

An interface can only extend an identifier/qualified-name with optional type arguments. ts(2499)

The express.RequestHandler interface does not support async functions. It says

The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>>'?ts(1064)

Here are my interfaces

export interface IRequest extends express.Request {
    user: IUser;
}

export interface IRequestHandler extends RequestHandler = (
    req: IRequest,
    res: express.Response,
    next: express.NextFunction
) => void | Promise<void>;

Advertisement

Answer

It seems you are trying to extend the express.Request interface.

Try:

import { NextFunction, Request, Response } from 'express';

interface IUser {
  name: string;
}

declare module 'express' {
  interface Request {
    user: IUser;
  }
}

const controller = async (req: Request, res: Response, next: NextFunction) => {
  console.log(req.user);
};

enter image description here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement