Skip to content

Commit 58aed8a

Browse files
committed
Convert SpawnSyncReturns to a discriminated union
1 parent 317215a commit 58aed8a

3 files changed

Lines changed: 63 additions & 24 deletions

File tree

types/node/index.d.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,14 +2331,27 @@ declare module "child_process" {
23312331
export interface SpawnSyncOptionsWithBufferEncoding extends SpawnSyncOptions {
23322332
encoding: string; // specify `null`.
23332333
}
2334-
export interface SpawnSyncReturns<T> {
2335-
pid: number;
2336-
output: [T | null, T | null, T | null] | null;
2337-
stdout: T | null;
2338-
stderr: T | null;
2339-
status: number | null;
2340-
signal: string | null;
2341-
error: Error | null;
2334+
export type SpawnSyncOptions = SpawnSyncReturns.WithError | SpawnSyncOptions.WithoutError<T>;
2335+
export namespace SpawnSyncReturns {
2336+
export interface Base {
2337+
pid: number;
2338+
}
2339+
export interface WithError extends Base {
2340+
error: Error;
2341+
output: null;
2342+
stdout: null;
2343+
stderr: null;
2344+
status: null;
2345+
signal: null;
2346+
}
2347+
export interface WithoutError<T> extends Base {
2348+
error: null;
2349+
output: [T | null, T | null, T | null];
2350+
stdout: T;
2351+
stderr: T;
2352+
status: number;
2353+
signal: string;
2354+
}
23422355
}
23432356
export function spawnSync(command: string): SpawnSyncReturns<Buffer>;
23442357
export function spawnSync(command: string, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;

types/node/v8/index.d.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,14 +2243,27 @@ declare module "child_process" {
22432243
export interface SpawnSyncOptionsWithBufferEncoding extends SpawnSyncOptions {
22442244
encoding: string; // specify `null`.
22452245
}
2246-
export interface SpawnSyncReturns<T> {
2247-
pid: number;
2248-
output: [T | null, T | null, T | null] | null;
2249-
stdout: T | null;
2250-
stderr: T | null;
2251-
status: number | null;
2252-
signal: string | null;
2253-
error: Error | null;
2246+
export type SpawnSyncOptions = SpawnSyncReturns.WithError | SpawnSyncOptions.WithoutError<T>;
2247+
export namespace SpawnSyncReturns {
2248+
export interface Base {
2249+
pid: number;
2250+
}
2251+
export interface WithError extends Base {
2252+
error: Error;
2253+
output: null;
2254+
stdout: null;
2255+
stderr: null;
2256+
status: null;
2257+
signal: null;
2258+
}
2259+
export interface WithoutError<T> extends Base {
2260+
error: null;
2261+
output: [T | null, T | null, T | null];
2262+
stdout: T;
2263+
stderr: T;
2264+
status: number;
2265+
signal: string;
2266+
}
22542267
}
22552268
export function spawnSync(command: string): SpawnSyncReturns<Buffer>;
22562269
export function spawnSync(command: string, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;

types/node/v9/index.d.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,14 +2323,27 @@ declare module "child_process" {
23232323
export interface SpawnSyncOptionsWithBufferEncoding extends SpawnSyncOptions {
23242324
encoding: string; // specify `null`.
23252325
}
2326-
export interface SpawnSyncReturns<T> {
2327-
pid: number;
2328-
output: [T | null, T | null, T | null] | null;
2329-
stdout: T | null;
2330-
stderr: T | null;
2331-
status: number | null;
2332-
signal: string | null;
2333-
error: Error | null;
2326+
export type SpawnSyncOptions = SpawnSyncReturns.WithError | SpawnSyncOptions.WithoutError<T>;
2327+
export namespace SpawnSyncReturns {
2328+
export interface Base {
2329+
pid: number;
2330+
}
2331+
export interface WithError extends Base {
2332+
error: Error;
2333+
output: null;
2334+
stdout: null;
2335+
stderr: null;
2336+
status: null;
2337+
signal: null;
2338+
}
2339+
export interface WithoutError<T> extends Base {
2340+
error: null;
2341+
output: [T | null, T | null, T | null];
2342+
stdout: T;
2343+
stderr: T;
2344+
status: number;
2345+
signal: string;
2346+
}
23342347
}
23352348
export function spawnSync(command: string): SpawnSyncReturns<Buffer>;
23362349
export function spawnSync(command: string, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;

0 commit comments

Comments
 (0)