英文:
typescript configure to get helmet import working
问题
所以我刚开始学习 TypeScript,遇到了一些问题。
所以我在项目中使用以下代码来导入 Helmet:
import * as helmet from "helmet";
但我一直遇到这个错误:
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';`
不要安装 @types/helmet,因为该包只是一个空存根。
我第一个解决方案是在导入行之前使用 // @ts-ignore,这样它就被“修复”了。但我查看了 Helmet 模块,并且它们确实提供了 .d.cts 和 .d.mts 文件,但是 ts-node 不认识它们,事实上,将一个文件从 index.d.cts 更改为 index.d.ts 确实解决了问题。
现在我怀疑这是我的 tsconfig 中的问题,有人可以帮助我吗?
以下是我的 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"]
}
编辑: 这个项目是使用 TypeORM init 和 Express 脚手架生成的,这里是我的设置示例,这可能会解释为什么我遇到这些问题。
英文:
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
答案1
得分: 1
你之所以收到此错误是因为你的 TypeScript 版本(4.5.2)与你的 Helmet 版本(7.0.0)不兼容/过旧。
请将 TypeScript 更新至最新版本(撰写本文时为5.1.6)以解决此问题:
npm i -D typescript@latest 或
pnpm add -D typescript@latest 或
yarn add -D typescript@latest
根据你选择的包管理器进行操作。 ![]()
来源:Helmet 仓库中的 dev 依赖项 "typescript": "^5.1.6"(撰写本文时)https://github.com/helmetjs/helmet/blob/main/package.json
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论