Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript issues #474

Open
selfagency opened this issue Sep 14, 2024 · 3 comments
Open

typescript issues #474

selfagency opened this issue Sep 14, 2024 · 3 comments

Comments

@selfagency
Copy link

sorry to say this has come up again

tsconfig

{
  "include": ["api/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"],
  "compilerOptions": {
    "target": "ES2020",
    "module": "Node16",
    "rootDir": "./api",
    "moduleResolution": "Node16",
    "types": [
      "node",
      "polka"
    ],
    "resolveJsonModule": true,
    "allowJs": true,
    "sourceMap": true,
    "outDir": "./dist",
    "importHelpers": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitAny": false,
    "skipLibCheck": true
  }
}

result

❯ ts-node-esm api/index.ts
/Users/daniel/Library/pnpm/global/5/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
api/index.ts:18:5 - error TS2349: This expression is not callable.
  Type 'typeof import("/Users/daniel/Developer/places-api/node_modules/.pnpm/[email protected]/node_modules/helmet/index")' has no call signatures.

18     helmet({
       ~~~~~~

    at createTSError (/Users/daniel/Library/pnpm/global/5/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:859:12)
    at reportTSError (/Users/daniel/Library/pnpm/global/5/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:863:19)
    at getOutput (/Users/daniel/Library/pnpm/global/5/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:1077:36)
    at Object.compile (/Users/daniel/Library/pnpm/global/5/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:1433:41)
    at transformSource (/Users/daniel/Library/pnpm/global/5/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/esm.ts:400:37)
    at /Users/daniel/Library/pnpm/global/5/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/esm.ts:278:53
    at async addShortCircuitFlag (/Users/daniel/Library/pnpm/global/5/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/esm.ts:409:15)
    at async nextLoad (node:internal/modules/esm/loader:163:22)
    at async ESMLoader.load (node:internal/modules/esm/loader:603:20)
    at async ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:11) {
  diagnosticCodes: [ 2349 ]
}
@EvanHahn
Copy link
Member

How are you importing Helmet? Could you show me a code snippet that can reproduce this problem?

@selfagency
Copy link
Author

selfagency commented Sep 16, 2024

import { City, Country, State } from 'country-state-city';
import helmet from 'helmet';
import polka from 'polka';
import bearerToken from 'polka-bearer-token';

const app = polka();
const port = process.env.PORT || 3000;

app
  .use((req, res, next) => {
    res.setHeader('Content-Type', 'application/json');
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    next();
  })
  .use(
    helmet({
      crossOriginResourcePolicy: false,
    })
  )
  .use(bearerToken())
  .use((req, res, next) => {
    const token = (req as typeof req & { token: string }).token.replace('Bearer ', '');
    if (token !== process.env.TOKEN) {
      res.statusCode = 401;
      res.end(JSON.stringify({ message: 'Unauthorized', status: 401 }));
    }
    next();
  })
  .get('/countries', (req, res) => {
    res.end(JSON.stringify(Country.getAllCountries()));
  })
  .get('/countries/:id', (req, res) => {
    res.end(JSON.stringify(Country.getCountryByCode(req.params.id)));
  })
  .get('/states', (req, res) => {
    res.end(JSON.stringify(State.getStatesOfCountry(req.query.country as string)));
  })
  .get('/states/:id', (req, res) => {
    res.end(JSON.stringify(State.getStateByCode(req.params.id)));
  })
  .get('/cities', (req, res) => {
    res.end(JSON.stringify(City.getCitiesOfState(req.query.country as string, req.query.state as string)));
  })
  .get('*', (req, res) => {
    res.statusCode = 404;
    res.end(JSON.stringify({ message: 'Not Found', status: 404 }));
  });

app.listen(port, () => {
  console.log(`Started on ${port}`);
});

@EvanHahn
Copy link
Member

I couldn't reproduce this in a reduced sample app. Here's what I have:

package.json:

{
  "private": true,
  "scripts": {
    "typecheck": "tsc --noEmit"
  },
  "dependencies": {
    "helmet": "^7.1.0"
  },
  "devDependencies": {
    "typescript": "^5.6.2"
  }
}

tsconfig.json (modified from yours):

{
  "include": ["app.ts"],
  "compilerOptions": {
    "target": "ES2020",
    "module": "Node16",
    "moduleResolution": "Node16",
    "types": [],
    "resolveJsonModule": true,
    "allowJs": true,
    "sourceMap": true,
    "importHelpers": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitAny": false,
    "skipLibCheck": true
  }
}

app.ts (simplified from yours):

import helmet from "helmet";

console.log(
  helmet({
    crossOriginResourcePolicy: false,
  }),
);

What versions are you using? Are any of your dependencies outdated? What about your version of Node.js?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants