Skip to content

Commit 607a0ff

Browse files
frolicehmicky
andauthored
define env type (#1141)
Co-authored-by: ehmicky <[email protected]>
1 parent c0b6efc commit 607a0ff

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

test-d/arguments/env.test-d.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import process, {type env} from 'node:process';
2+
import {expectType, expectAssignable} from 'tsd';
3+
import {execa, type Options, type Result} from '../../index.js';
4+
5+
type NodeEnv = 'production' | 'development' | 'test';
6+
7+
// Libraries like Next.js or Remix do the following type augmentation.
8+
// The following type tests ensure this works with Execa.
9+
// See https://github.com/sindresorhus/execa/pull/1141 and https://github.com/sindresorhus/execa/issues/1132
10+
declare global {
11+
// eslint-disable-next-line @typescript-eslint/no-namespace
12+
namespace NodeJS {
13+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
14+
interface ProcessEnv {
15+
readonly NODE_ENV: NodeEnv;
16+
}
17+
}
18+
}
19+
20+
// The global types are impacted
21+
expectType<NodeEnv>(process.env.NODE_ENV);
22+
expectType<NodeEnv>('' as (typeof env)['NODE_ENV']);
23+
expectType<NodeEnv>('' as NodeJS.ProcessEnv['NODE_ENV']);
24+
expectType<NodeEnv>('' as globalThis.NodeJS.ProcessEnv['NODE_ENV']);
25+
26+
// But Execa's types are not impacted
27+
expectType<string | undefined>('' as Exclude<Options['env'], undefined>['NODE_ENV']);
28+
expectAssignable<Result>(await execa({env: {test: 'example'}})`unicorns`);
29+
expectAssignable<Result>(await execa({env: {test: 'example'} as const})`unicorns`);
30+
expectAssignable<Result>(await execa({env: {test: undefined}})`unicorns`);
31+
expectAssignable<Result>(await execa({env: {test: undefined} as const})`unicorns`);

types/arguments/options.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type {SignalConstants} from 'node:os';
2-
import type {env} from 'node:process';
32
import type {Readable} from 'node:stream';
43
import type {Unless} from '../utils.js';
54
import type {Message} from '../ipc.js';
@@ -77,7 +76,7 @@ export type CommonOptions<IsSync extends boolean = boolean> = {
7776
7877
@default [process.env](https://nodejs.org/api/process.html#processenv)
7978
*/
80-
readonly env?: typeof env;
79+
readonly env?: Readonly<Partial<Record<string, string>>>;
8180

8281
/**
8382
If `true`, the subprocess uses both the `env` option and the current process' environment variables ([`process.env`](https://nodejs.org/api/process.html#processenv)).

0 commit comments

Comments
 (0)