In my current typescript project utilizing express
for backend API calls, the issue arises when attempting to assign custom properties. Typescript throws an error indicating that these properties do not exist.
To address this, I created a custom types
subdirectory within the project structure. This subdirectory is intended to manage additional attributes and custom types that might be added later in the project's lifecycle. The project setup looks like this:
src
│ index.ts
│
├───config
│ database.ts
│
├───routes
│ login.ts
│ router.ts
│ signup.ts
│ upload.ts
│
├───types
│ express.d.ts
│
└───utils
auth.ts
All other configuration files are located at the top level (such as package.json
or package-lock.json
). To incorporate the 'types' subdirectory, modifications were made to the tsconfig.json file with the following settings:
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"typeRoots": [
"./node_modules/@types",
"./src/types"
]
},
"exclude": [
"node_modules",
"dist"
]
}
The type file includes the following content:
// express.d.ts
import { Request } from 'express';
interface User {
id: number,
username: string,
token_type: string
}
declare global {
namespace Express {
interface Request {
files?: any,
user: User
}
}
}
Despite these settings, an error occurs when trying to access or modify the newly assigned attributes, indicating that they do not exist:
C:\Users\Me\AppData\Roaming\npm\node_modules\ts-node\src\index.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:\Users\Me\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:859:12)
at reportTSError (C:\Users\Me\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:863:19)
at getOutput (C:\Users\Me\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1077:36)
at Object.compile (C:\Users\Me\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1433:41)
at Module.m._compile (C:\Users\Me\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1617:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
at Object.require.extensions.<computed> [as .ts] (C:\Users\Me\AppData\Roaming\npm\node_modules\ts-node\src\index.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 have searched online for solutions without success. Any assistance would be greatly appreciated! :D