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! 😀