After successfully augmenting an existing interface (Express Request
as shown here) with a custom type definition, I encountered an issue. Originally, the property name
was defined as a string within the express.d.ts
:
declare namespace Express {
export interface Request {
name: string
}
}
However, I now require the name
to be of type MyClass
, a custom class with properties first
and last
. The class is defined as follows:
export class MyClass {
first: string;
last: string;
}
To accommodate this change, I updated the interface augmentation as shown below:
import { MyClass } from "../routes/myClass";
declare namespace Express {
export interface Request {
name: MyClass
}
}
Unfortunately, upon accessing req.name
, I encountered the following error:
error TS2339: Property 'name' does not exist on type 'Request'.
Upon further investigation, I realized that my express.d.ts
file became a “module” due to the added import
statement. Despite this realization, I am still unsure how to resolve this dilemma.