Thanks to visit codestin.com
Credit goes to github.com

Skip to content

allowAutomaticSingleRunInference breaks monorepo compilerOptions.types #4435

Closed
@aaronadamsCA

Description

@aaronadamsCA
  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

With allowAutomaticSingleRunInference: true enabled in a medium-sized React monorepo, compilerOptions.types no longer works as expected in one of our packages. My best guess is that the package is being linted using the wrong tsconfig.

Repro

With this config:

/packages/components/tsconfig.json
{
  "extends": "../../tsconfig.json",
  "include": ["./src/**/*"],
  "compilerOptions": {
    "types": ["segment-analytics"]
  }
}

And this line of code:

window.analytics.track(process.env["FOO"] ?? "foo");

Expected behaviour

Without allowAutomaticSingleRunInference, @typescript-eslint/no-unsafe-member-access gives us exactly the error we'd expect:

Unsafe member access .env on an `any` value

That's because I didn't include "node" in compilerOptions.types, so process.env is untyped.

Actual behaviour

With allowAutomaticSingleRunInference: true, it's a different story:

Unsafe member access .track on an `any` value

Oops. Somehow process.env is typed, but window.analytics is untyped. Both are incorrect; so clearly our compilerOptions.types isn't being respected.

No project references. We do use paths but they're not involved in this specific situation AFAIK. Our tsconfig.json inheritance is straightforward; I've included more of our config info below.

This reliably reproduces both locally and in CI, so I'm reasonably confident it isn't an environment issue.

Happy to hear any ideas RE: how we might create a standalone repro or help debug the issue. Access to our private repo could be an option, too.

Additional Info

/tsconfig.json
{
  "compilerOptions": {
    "baseUrl": "./",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "incremental": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "module": "ESNext",
    "moduleResolution": "Node",
    "noEmit": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noUncheckedIndexedAccess": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "paths": {
      "@reach/router": ["./node_modules/@gatsbyjs/reach-router/es"],
      "@shopnishe/components": ["./packages/components/src"]
      // Our other internal packages
    },
    "skipLibCheck": true,
    "strict": true,
    "target": "ESNext"
  }
}
/.eslintrc.cjs
module.exports = {
  extends: "./.eslintrc.base.cjs",
  overrides: [
    {
      files: ["*.ts", "*.tsx"],
      parserOptions: {
        allowAutomaticSingleRunInference: true,
        tsconfigRootDir: __dirname,
        project: ["./apps/*/tsconfig.json", "./packages/*/tsconfig.json"],
      },
      extends: [
        "plugin:@typescript-eslint/recommended-requiring-type-checking",
      ],
      rules: {
        // Our typed TypeScript rules
      },
    },
  ],
};
/.eslintrc.base.cjs
module.exports = {
  root: true,
  extends: [
    "eslint:recommended",
    "plugin:react/recommended",
    "prettier", // Disables conflicting rules in above configs
    "plugin:react-hooks/recommended",
    "plugin:eslint-comments/recommended",
    "plugin:import/recommended",
    "plugin:jsx-a11y/strict",
  ],
  parser: "@babel/eslint-parser",
  parserOptions: {
    sourceType: "module",
  },
  env: {
    browser: true,
    es2020: true,
    node: true,
  },
  plugins: ["simple-import-sort"],
  rules: {
    // Our base rules
  },
  overrides: [
    {
      // Make sure ESLint can see every possible Node/TypeScript file
      files: ["*.cjs", "*.js", "*.jsx", "*.mjs", "*.ts", "*.tsx"],
    },
    {
      files: ["*.ts", "*.tsx"],
      extends: [
        "plugin:@typescript-eslint/recommended",
        "plugin:import/typescript",
      ],
      plugins: ["eslint-plugin-tsdoc"],
      rules: {
        // Our non-typed TypeScript rules
      },
    },
  ],
  settings: {
    // Our eslint-plugin-import settings
    react: {
      version: "detect",
    },
  },
};

Versions

package version
@typescript-eslint/typescript-estree 5.9.0
TypeScript 4.5.4
node 16.13.1
Environment Info:

Node version: v16.13.1
npm version: v8.1.2
Local ESLint version: v8.6.0 (Currently used)
Global ESLint version: Not found
Operating System: linux 4.19.128-microsoft-standard

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting responseIssues waiting for a reply from the OP or another partypackage: typescript-estreeIssues related to @typescript-eslint/typescript-estree

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions