jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: @jest/types pollutes global typescript lib definitions with latest node types.

Open betaorbust opened this issue 2 years ago • 0 comments

Version

^29.3.1, but I believe this impacts many/most version with type support.

Steps to reproduce

  1. Clone this repo https://github.com/betaorbust/jest-type-pollution
  2. Repro steps in Readme.md, but in short:
    # Produces no type error when one would be expected
    npm install && npm run build
    
    # Remove @jest/types with global @types/node subdependency
    npm uninstall @jest/types
    rm -rf node_modules
    
    # Throws expected type error
    npm install && npm run build
    

Expected behavior

A repo using @jest/types should have the global type definitions defined and constrained by its tsconfig.json's target and lib entries.

tsconfig.json:

{
    "compilerOptions": {
        "outDir": "./dist",
        "rootDir": "./src",
        "target": "ES5",
        "lib": ["ES5", "DOM"]
    }
}

src/index.ts

// This should throw a type error, but does not
Object.fromEntries((['test', 'value']]);

Actual behavior

Global types are for latest node version, independent of project configuration, because @jest/types has a dependency on "@types/node":"*" which globally installs the latest node type definitions for the entire repo (or even monorepo due to package resolution.)

So every package in our monorepo believes it's running with library support provided by latest node (18.11.18 at the time of writing) which has caused several production issues where typescript doesn't catch an engineer adding language feature dependencies that our browser matrix can not handle.

Additional context

Prior art:

  • https://github.com/facebook/jest/issues/8092
  • https://github.com/facebook/jest/pull/10937

Environment

System:
    OS: macOS 12.6.2
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 16.18.0 - ~/.nvm/versions/node/v16.18.0/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v16.18.0/bin/npm

betaorbust avatar Jan 18 '23 07:01 betaorbust