Skip to content

Overriding Node's ProcessEnv typing causes TypeScript errors in third-party libs #8379

@kachkaev

Description

@kachkaev

Bug report

Describe the bug

https://github.com/zeit/next.js/blob/cffa2e3f2165b58b77fb354d3f74a4ac8222b68f/packages/next/types/global.d.ts#L1-L12


This file overrides the original implementation of the ProcessEnv typing, which causes negative side effects in third-party libraries that rely on it. In my case this was execa.

To Reproduce

  1. Create a Next.js app, ensure tsconfig.json and next-env.d.ts have been autogenerated.

  2. Install execa and ts-node.

    yarn add execa ts-node
    
  3. In test.ts write something like:

    import execa from "execa";
    
    execa("printenv", [], {
        env: {
          HELLO: "world",
          NODE_ENV: "development",
        },
        extendEnv: false,
        stdout: "inherit",
      });
  4. Observe no typing issues and ensure that the command works by running:

    yarn ts-node --transpile-only  --compiler-options="{\"module\": \"commonjs\"}" test.ts
    

    Output:

    HELLO=world
    NODE_ENV=development
    ✨  Done in 0.42s.
    
  5. Comment out NODE_ENV: "development", in test.ts, which is a totally valid action.

  6. Observe the following TypeScript error in the editor:

    Argument of type '{ env: { HELLO: string; }; extendEnv: false; stdout: "inherit"; }' is not assignable to parameter of type 'Options<null>'.
      Types of property 'env' are incompatible.
        Property 'NODE_ENV' is missing in type '{ HELLO: string; }' but required in type 'ProcessEnv'.ts(2345)
    global.d.ts(10, 14): 'NODE_ENV' is declared here.
    

Expected behavior

I would not expect Next.js to pollute Node's ProcessEnv interface, thus not causing typing false-positives in third-party libraries like execa. I'm using execa in a project that includes a few data-processing commands in addition to the Next.js app itself.

System information

  • Version of Next.js: 9.0.3

Additional context

git blame points to #7648

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions