slebetman / express-htmx-components

Simple htmx component server for Express
BSD 2-Clause "Simplified" License
2 stars 0 forks source link

Adding `express-htmx-components` dependency breaks `express.static` type #1

Open mleonhard opened 6 months ago

mleonhard commented 6 months ago

I followed getting-started.md but it yields a broken installation. It appears that adding express-htmx-components to package.json causes TypeScript errors on express.static and other functions in the express module.

% npx ts-node server.ts
/Users/user/app/node_modules/ts-node/src/index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
server.ts:6:20 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'RequestHandler<Response<any, Record<string, any>>>' is not assignable to parameter of type 'RequestHandlerParams'.
      Type 'RequestHandler<Response<any, Record<string, any>>>' is not assignable to type 'RequestHandler'.
        Types of parameters 'request' and 'req' are incompatible.
          Type 'Request' is missing the following properties from type 'IncomingMessage': aborted, httpVersion, httpVersionMajor, httpVersionMinor, and 65 more.

6 app.use('/static', express.static('static'));
                     ~~~~~~~~~~~~~~~~~~~~~~~~

  node_modules/express-serve-static-core/express-serve-static-core.d.ts:38:9
    38         (path: PathParams, ...handlers: RequestHandlerParams[]): T;
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The last overload is declared here.

    at createTSError (/Users/user/app/node_modules/ts-node/src/index.ts:859:12)
    at reportTSError (/Users/user/app/node_modules/ts-node/src/index.ts:863:19)
    at getOutput (/Users/user/app/node_modules/ts-node/src/index.ts:1077:36)
    at Object.compile (/Users/user/app/node_modules/ts-node/src/index.ts:1433:41)
    at Module.m._compile (/Users/user/app/node_modules/ts-node/src/index.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/user/app/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1206:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1022:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) {
  diagnosticCodes: [ 2769 ]
}

package.json:

{
  "name": "app",
  "private": true,
  "description": "web server",
  "version": "1.0.0",
  "main": "dist/server.js",
  "scripts": {
    "build": "npx tsc",
    "dev": "nodemon server.ts",
    "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
    "start": "node dist/server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "@types/express": "^4.17.1",
    "@types/node": "^20.12.11",
    "@typescript-eslint/eslint-plugin": "^7.8.0",
    "@typescript-eslint/parser": "^7.8.0",
    "concurrently": "^8.2.2",
    "dotenv": "^16.4.5",
    "eslint": "^8.57.0",
    "express": "^4.17.1",
    "express-session": "^1.18.0",
    "express-htmx-components": "^1.2.9",
    "nodemon": "^3.1.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.4.5"
  }
}

server.ts:

#! /usr/bin/env node
import express from 'express';

const app = express();
const PORT = 3000;
app.use('/static', express.static('static'));
app.listen(PORT, () => {
    console.log(`[server]: Server is running at http://localhost:${PORT}`);
});

tsconfig.json:

{
  "compilerOptions": {
    "target": "es2022",
    "module": "commonjs",
    "outDir": "./dist/",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}
mleonhard commented 6 months ago

I think the root cause is this incorrect dependency on express-serve-static-core: https://github.com/slebetman/express-htmx-components/blob/0342ed609fb44eff47ecbc53c6d1636e6f864de4/package.json#L12-L15