From 6cc519b4c9a0868e89a2104de468db5cc6fd8d32 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Thu, 9 May 2024 18:07:57 +0100 Subject: [PATCH] Fix complexity bug with types (#1037) --- types/stdio/direction.d.ts | 12 +++--------- types/stdio/type.d.ts | 15 +++++++++++---- types/transform/object-mode.d.ts | 7 ++----- types/utils.d.ts | 4 ++++ 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/types/stdio/direction.d.ts b/types/stdio/direction.d.ts index 63541f4972..b58b89a916 100644 --- a/types/stdio/direction.d.ts +++ b/types/stdio/direction.d.ts @@ -1,5 +1,6 @@ import type {CommonOptions} from '../arguments/options.js'; -import type {StdinOptionCommon, StdoutStderrOptionCommon, StdioOptionCommon} from './type.js'; +import type {Intersects} from '../utils.js'; +import type {StdioSingleOptionItems, InputStdioOption} from './type.js'; import type {FdStdioArrayOption} from './option.js'; // Whether `result.stdio[FdNumber]` is an input stream @@ -8,11 +9,4 @@ export type IsInputFd< OptionsType extends CommonOptions = CommonOptions, > = FdNumber extends '0' ? true - : IsInputDescriptor>; - -// Whether `result.stdio[3+]` is an input stream -type IsInputDescriptor = StdioOptionType extends StdinOptionCommon - ? StdioOptionType extends StdoutStderrOptionCommon - ? false - : true - : false; + : Intersects>, InputStdioOption>; diff --git a/types/stdio/type.d.ts b/types/stdio/type.d.ts index 1a3ded7e56..5b890395bc 100644 --- a/types/stdio/type.d.ts +++ b/types/stdio/type.d.ts @@ -67,10 +67,10 @@ type ProcessStdinFd = {readonly fd?: 0}; type ProcessStdoutStderrFd = {readonly fd?: 1 | 2}; // Values available only in `options.stdin|stdio` -type InputStdioOption< - IsSync extends boolean, - IsExtra extends boolean, - IsArray extends boolean, +export type InputStdioOption< + IsSync extends boolean = boolean, + IsExtra extends boolean = boolean, + IsArray extends boolean = boolean, > = | 0 | Unless, Uint8Array | IterableObject> @@ -145,6 +145,13 @@ export type StdioSingleOption< | StdinSingleOption | StdoutStderrSingleOption; +// Get `options.stdin|stdout|stderr|stdio` items if it is an array, else keep as is +export type StdioSingleOptionItems< + StdioOptionType extends StdioOptionCommon, +> = StdioOptionType extends readonly StdioSingleOption[] + ? StdioOptionType[number] + : StdioOptionType; + // `options.stdin|stdout|stderr|stdio` export type StdioOptionCommon = | StdinOptionCommon diff --git a/types/transform/object-mode.d.ts b/types/transform/object-mode.d.ts index 82f4619fea..1503f7c0ee 100644 --- a/types/transform/object-mode.d.ts +++ b/types/transform/object-mode.d.ts @@ -1,4 +1,4 @@ -import type {StdioSingleOption, StdioOptionCommon} from '../stdio/type.js'; +import type {StdioSingleOption, StdioOptionCommon, StdioSingleOptionItems} from '../stdio/type.js'; import type {FdStdioOption} from '../stdio/option.js'; import type {CommonOptions} from '../arguments/options.js'; import type {DuplexTransform} from './normalize.js'; @@ -10,10 +10,7 @@ export type IsObjectFd< OptionsType extends CommonOptions = CommonOptions, > = IsObjectStdioOption>; -type IsObjectStdioOption = IsObjectStdioSingleOption; +type IsObjectStdioOption = IsObjectStdioSingleOption>; type IsObjectStdioSingleOption = StdioSingleOptionType extends {objectMode?: boolean} ? BooleanObjectMode diff --git a/types/utils.d.ts b/types/utils.d.ts index 731d646ade..23871cf80e 100644 --- a/types/utils.d.ts +++ b/types/utils.d.ts @@ -7,3 +7,7 @@ export type Or = First extends tr export type Unless = Condition extends true ? ElseValue : ThenValue; export type AndUnless = Condition extends true ? ElseValue : ThenValue; + +// Whether any of T's union element is the same as one of U's union element. +// `&` does not work here. +export type Intersects = true extends (T extends U ? true : false) ? true : false;