打字稿会忽略node_module的tsconfig

时间:2020-06-22 13:42:41

标签: typescript

在我的项目中,我在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,这意味着其他严格检查选项为false。由于直接在node-expose-sspi文件夹中运行tsc不会产生任何错误,但是从我的项目文件夹中失败。

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

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

编辑:

我的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更改为示例的精确副本。

对不起,我在最初的问题中弄错了。 当我使用const { sso } = require('node-expose-sspi');时,我不会从打字稿中得到错误。 实际上,TS完全忽略了此模块,并且出现以下错误:

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

req.sso行中:Property 'sso' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.

但是,当我更改为import { sso } from "node-expose-sspi";时,会遇到与模块相关的62个错误。

2 个答案:

答案 0 :(得分:0)

将此文件添加到tsconfig文件中

"exclude": [
 "node_modules"
]

答案 1 :(得分:0)

我最终用const { sso } = require('node-expose-sspi');导入了sspi。这样typescript不会检查此模块。 唯一的缺点是typescript无法识别sso中的request。对我来说,这不是一个大问题。我只需要sso即可在Intranet应用程序中获取用户名。 因此,我只是在有问题的行之前添加了@ts-ignore

//@ts-ignore
const username = request.sso.user.name;