node.jstypescripttsconfighelmet.js

typescript configure to get helmet import working


so i'm just getting into typescript and i've ran into my first few stumbles. so i use this line to import helmet in my project:

import * as helmet from "helmet";

but i keep running into this error

src/index.ts:3:25 - error TS7016: Could not find a declaration file for module 'helmet'. 'D:/Dev/ticktack/node_modules/helmet/index.cjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/helmet` if it exists or add a new declaration (.d.ts) file containing `declare module 'helmet';`

and no, installing @types/helmet won't work since that package is just an empty stub. MY first solution i came up with was to use // @ts-ignore before the import line wish "fixed" it. But it irked i went to look into the helmet module and they do indeed provide .d.cts and .d.mts files but ts-node doesn't recognize them, in fact just changing one from index.d.cts to index.d.ts truly fixed it.

now i suspect this is a problem in my tsconfig so can anyone please help me?!

here's my tsconfig:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": ["dom", "es6", "es2017"],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
    "removeComments": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "baseUrl": ".",
    "typeRoots": ["./node_modules/@types", "src/types"]
  },
  "exclude": ["node_modules"],
  "include": ["./src/**/*.tsx", "./src/**/*.ts"]
}

Edit: this project was scaffolded using typeORM init with express, here's a sapmle of my setup. this might shed some light on the reason why i'm having this issues


Solution

  • You're getting this error because you're using an incompatible/old version of TypeScript (4.5.2) with your version of Helmet (7.0.0).

    Update TypeScript to the latest version (5.1.6 as of writing this) to resolve the issue:

    npm i -D typescript@latest or

    pnpm add -D typescript@latest or

    yarn add -D typescript@latest

    Depending on your package manager of choice. :)

    Source: "typescript": "^5.1.6" as a dev dependency in the Helmet repo (as of writing this) https://github.com/helmetjs/helmet/blob/main/package.json