what's wrong here admin@Admins-MacBook-Pro server % npm run start server@1.0.0 start…
- User
what's wrong here
admin@Admins-MacBook-Pro server % npm run start
server@1.0.0 start ts-node index.ts
/Users/admin/Documents/Sapphire/server/node_modules/ts-node/src/index.ts:859 return new TSError(diagnosticText, diagnosticCodes, diagnostics); ^ TSError: ⨯ Unable to compile TypeScript: index.ts:10:9 - error TS2769: No overload matches this call. The last overload gave the following error. Argument of type '(req: Request, res: Response, next: NextFunction) => void' is not assignable to parameter of type 'PathParams'.
10 app.use((req: Request, res: Response, next: NextFunction) => { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:157:5 157 < ~ 158 P = ParamsDictionary, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ... 166 ...handlers: Array<RequestHandlerParams<P, ResBody, ReqBody, ReqQuery, LocalsObj>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 167 ): T; ~~~~~~~~~ The last overload is declared here. index.ts:10:45 - error TS2552: Cannot find name 'NextFunction'. Did you mean 'Function'?
10 app.use((req: Request, res: Response, next: NextFunction) => { ~~~~~~~~~~~~
node_modules/typescript/lib/lib.es5.d.ts:319:13 319 declare var Function: FunctionConstructor; ~~~~~~~~ 'Function' is declared here. index.ts:12:20 - error TS2339: Property 'path' does not exist on type 'Request'.
12 const path = req.path; ~~~~ index.ts:16:3 - error TS2322: Type '(bodyJson: Record<string, any>, ...args: any[]) => any' is not assignable to type '() => Promise'. Target signature provides too few arguments. Expected 1 or more, but got 0.
16 res.json = function (bodyJson: Record<string, any>, ...args: any[]) { ~~~~~~~~ index.ts:21:7 - error TS2339: Property 'on' does not exist on type 'Response'.
21 res.on("finish", () => { ~~ index.ts:24:50 - error TS2339: Property 'statusCode' does not exist on type 'Response'.
24 let logLine =
${req.method} ${path} ${res.statusCode} in ${duration}ms; ~~~~~~~~~~ index.ts:41:47 - error TS2345: Argument of type 'Application' is not assignable to parameter of type 'Express'. Type 'Application<Record<string, any>>' is missing the following properties from type 'Express': request, response41 const server: Server = await registerRoutes(app); ~~~ index.ts:43:11 - error TS2769: No overload matches this call. The last overload gave the following error. Argument of type '(err: any, _req: Request, res: Response, _next: NextFunction) => never' is not assignable to parameter of type 'PathParams'.
43 app.use((err: any, _req: Request, res: Response, _next: NextFunction) => { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:157:5 157 < ~ 158 P = ParamsDictionary, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ... 166 ...handlers: Array<RequestHandlerParams<P, ResBody, ReqBody, ReqQuery, LocalsObj>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 167 ): T; ~~~~~~~~~ The last overload is declared here. index.ts:43:59 - error TS2552: Cannot find name 'NextFunction'. Did you mean 'Function'?
43 app.use((err: any, _req: Request, res: Response, _next: NextFunction) => { ~~~~~~~~~~~~
node_modules/typescript/lib/lib.es5.d.ts:319:13 319 declare var Function: FunctionConstructor; ~~~~~~~~ 'Function' is declared here. index.ts:47:9 - error TS2349: This expression is not callable. Type 'Number' has no call signatures.
47 res.status(status).json({ message }); ~~~~~~ index.ts:52:21 - error TS2345: Argument of type 'Application' is not assignable to parameter of type 'Express'. Type 'Application<Record<string, any>>' is missing the following properties from type 'Express': request, response
52 await setupVite(app, server); ~~~ index.ts:54:17 - error TS2345: Argument of type 'Application' is not assignable to parameter of type 'Express'. Type 'Application<Record<string, any>>' is missing the following properties from type 'Express': request, response
54 serveStatic(app); ~~~ index.ts:1:8 - error TS1109: Expression expected.
1 import import express, { Application } from 'express'; ~~~~~~
at createTSError (/Users/admin/Documents/Sapphire/server/node_modules/ts-node/src/index.ts:859:12) at reportTSError (/Users/admin/Documents/Sapphire/server/node_modules/ts-node/src/index.ts:863:19) at getOutput (/Users/admin/Documents/Sapphire/server/node_modules/ts-node/src/index.ts:1077:36) at Object.compile (/Users/admin/Documents/Sapphire/server/node_modules/ts-node/src/index.ts:1433:41) at Module.m._compile (/Users/admin/Documents/Sapphire/server/node_modules/ts-node/src/index.ts:1617:30) at node:internal/modules/cjs/loader:1706:10 at Object.require.extensions.<computed> [as .ts] (/Users/admin/Documents/Sapphire/server/node_modules/ts-node/src/index.ts:1621:12) at Module.load (node:internal/modules/cjs/loader:1289:32) at Function._load (node:internal/modules/cjs/loader:1108:12) at TracingChannel.traceSync (node:diagnostics_channel:322:14) {diagnosticCodes: [ 2769, 2552, 2339, 2322, 2339, 2339, 2345, 2769, 2552, 2349, 2345, 2345, 1109 ] } admin@Admins-MacBook-Pro server %
- Assistant
The error output you're seeing indicates multiple issues in your TypeScript code related to Express, and they predominantly stem from TypeScript type definitions and imports. Here are the steps to troubleshoot and potentially fix the problems:
-
Check Your Imports: There's an apparent syntax error at the top of your file:
import import express, { Application } from 'express';This should instead be:
import express, { Application, Request, Response, NextFunction } from 'express';Make sure all necessary types (
Request,Response,NextFunction) are imported correctly. -
Type Definitions for Express: Make sure you have the necessary type definitions installed. For Express, you should have:
npm install @types/express --save-devThis provides the TypeScript type definitions needed to correctly type your Express middleware.
-
Middleware Type Issues: Your middleware function needs to correctly match the express handler signature. If you see errors like:
Argument of type '(req: Request, res: Response, next: NextFunction) => void' is not assignable to parameter of type 'PathParams'.It indicates that TypeScript cannot recognize the input correctly. The imports fixed above should help alleviate this.
-
Using
res.json: The error regardingres.json:res.json = function (bodyJson: Record<string, any>, ...args: any[]) {This line is incorrect as it attempts to overwrite the
jsonfunction. Instead, you would normally just callres.json(bodyJson)without redefining it. -
Request and Response Properties: Errors like:
Property 'path' does not exist on type 'Request'.This can happen if there's a mismatch in expected types; ensure that your imports and type definitions are up to date. Make sure your Express version and type definitions match.
-
Error Handlers: Your error handling middleware should properly match the types:
app.use((err: any, _req: Request, res: Response, _next: NextFunction) => { // handle the error });Ensure all parameters are correctly typed.
-
Ensure Cleanup: After making changes, make sure to clean and rebuild your TypeScript project to see if the errors persist. You can do this with:
npm run clean npm run build -
Compatibility: Make sure that your version of TypeScript and the Express types are compatible. Sometimes mismatched versions can lead to these typing issues. Consider updating
typescriptas well:npm install typescript --save-dev
After making these changes, run your server again using
npm run startand check whether the issues persist. If they do, it may be helpful to inspect specific line numbers mentioned in the error output for targeted debugging. -