Typescript not recognizing type files when trying to extend Express’ `Request` object to incl.ude custom properties for middleware functions

I am currently working on a typescript project that uses express to manage backend API calls, however when trying to assign these custom properties, typescript raises an error stating that these properties simply do not exist.

I have tried to create a custom types subdirectory within my project to manage all the extra attributes as well as several custom types I may add later on during the project’s development. I have tried this and setup the project like so

src
│   index.ts
│   
├───config
│       database.ts
│       
├───routes
│       login.ts
│       router.ts
│       signup.ts
│       upload.ts
│       
├───types
│       express.d.ts
│
└───utils
        auth.ts

With all other configuration files located at the top level (such as package.json or package-lock.json). To add the types subdirectory I have modified my tsconfig.json with the following contents

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "typeRoots": [
      "./node_modules/@types",
      "./src/types"
    ]
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

And the type file with the contents:

// express.d.ts
import { Request } from 'express';

interface User {
    id: number,
    username: string,
    token_type: string
}

declare global {
    namespace Express {
        interface Request {
            files?: any,// Replace `any` with the appropriate type if you know it
            user: User
        }
    }
}

But an error is raised when trying to access or modify the newly assigned attributes essentially saying it doesn’t exist

C:UsersMeAppDataRoamingnpmnode_modulests-nodesrcindex.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/routes/upload.ts:29:14 - error TS2339: Property 'files' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.

29     if (!req.files || Object.keys(req.files).length === 0) {
                ~~~~~
src/routes/upload.ts:29:39 - error TS2339: Property 'files' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.

29     if (!req.files || Object.keys(req.files).length === 0) {
                                         ~~~~~
src/routes/upload.ts:32:22 - error TS2339: Property 'files' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.

32     const file = req.files.Filename
                        ~~~~~
src/routes/upload.ts:37:13 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.

37         req.user!.id,
               ~~~~

    at createTSError (C:UsersMeAppDataRoamingnpmnode_modulests-nodesrcindex.ts:859:12)
    at reportTSError (C:UsersMeAppDataRoamingnpmnode_modulests-nodesrcindex.ts:863:19)
    at getOutput (C:UsersMeAppDataRoamingnpmnode_modulests-nodesrcindex.ts:1077:36)
    at Object.compile (C:UsersMeAppDataRoamingnpmnode_modulests-nodesrcindex.ts:1433:41)
    at Module.m._compile (C:UsersMeAppDataRoamingnpmnode_modulests-nodesrcindex.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Object.require.extensions.<computed> [as .ts] (C:UsersMeAppDataRoamingnpmnode_modulests-nodesrcindex.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Function.Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19) {
  diagnosticCodes: [ 2339, 2339, 2339, 2339 ]
}

I’ve been looking online and can’t seem to find the answer, all help is appreciated! 😀

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật