【问题标题】:Typescript ignores node_module's tsconfig打字稿忽略 node_module 的 tsconfig
【发布时间】:2020-10-12 09:34:03
【问题描述】:

在我的项目中,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?

我可以在编译 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 完全相同的 exaple 副本。

抱歉,我在最初的问题中犯了错误。 当我使用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 个与模块相关的错误。

【问题讨论】:

  • 它在反应项目中吗?
  • 不,只是 node.js。

标签: typescript


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    将此添加到tsconfig 文件中

    "exclude": [
     "node_modules"
    ]
    

    【讨论】:

    • exclude 在 ts 文件中导入模块时不起作用。
    猜你喜欢
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 2020-01-13
    • 2021-07-24
    • 2018-05-16
    • 2017-04-25
    相关资源
    最近更新 更多