首页 > 解决方案 > 打字稿忽略 node_module 的 tsconfig

问题描述

在我的项目中,tsconfig.json我选择了选项strict=true(启用所有严格的类型检查选项)。

当我添加时,import { sso } from 'node-expose-sspi';我从 tsc 收到大约 60 个错误。

例如: src/sso/auth.ts line 88 waitForReleased accepts string but cookieToken is string | undefined

我注意到,在 node-expose-sspi tsconfig 中只有noImplicitAny=true,这意味着其他严格检查选项为假。由于直接在 node-expose-sspi 文件夹中运行 tsc 不会产生错误,但它从我的项目文件夹中失败。

但是为什么 typescript 编译器会忽略特定于模块的 tsconfig.json?

./node_modules/xxx/tsconfig.json我可以在编译 xxx 模块时以某种方式强制使用顶级 tsc吗?

编辑:

我的 tsconfig.json:

{
  "compilerOptions": {
    "target": "es2020",
    "module": "commonjs",
    "outDir": "./dist/",
    "rootDir": "./",
    "removeComments": true,
    "noEmitOnError": true,

    "strict": true,

    "moduleResolution": "node",
    "esModuleInterop": true,

    "forceConsistentCasingInFileNames": true
  }
}

和 server.ts:

import express = require('express');
import { sso } from 'node-expose-sspi';

const app = express();

app.use(sso.auth());

app.use((req, res, next) => {
  res.json({
    sso: req.sso,
  });
});

app.listen(3001, () => console.log('Server started on port 3001'));

编辑:我已将 server.ts 更改为node-expose-sspi中 exaple 的精确副本。

对不起,我在最初的问题中犯了错误。当我使用时,const { sso } = require('node-expose-sspi');我不会从打字稿中得到错误。事实上,TS 完全忽略了这个模块,我得到了错误:

app.use((req, res, next) => {
  res.json({
    sso: req.sso,
  });
});

在线req.ssoProperty 'sso' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.

但是,一旦我更改为import { sso } from "node-expose-sspi";我得到 62 与模块相关的错误。

标签: typescript

解决方案


tsconfig在文件中添加这个

"exclude": [
 "node_modules"
]

推荐阅读