Skip to content

Commit

Permalink
Fix complexity bug with types (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored May 9, 2024
1 parent 398d090 commit 6cc519b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
12 changes: 3 additions & 9 deletions types/stdio/direction.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,11 +9,4 @@ export type IsInputFd<
OptionsType extends CommonOptions = CommonOptions,
> = FdNumber extends '0'
? true
: IsInputDescriptor<FdStdioArrayOption<FdNumber, OptionsType>>;

// Whether `result.stdio[3+]` is an input stream
type IsInputDescriptor<StdioOptionType extends StdioOptionCommon> = StdioOptionType extends StdinOptionCommon
? StdioOptionType extends StdoutStderrOptionCommon
? false
: true
: false;
: Intersects<StdioSingleOptionItems<FdStdioArrayOption<FdNumber, OptionsType>>, InputStdioOption>;
15 changes: 11 additions & 4 deletions types/stdio/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<And<IsSync, IsExtra>, Uint8Array | IterableObject<IsArray>>
Expand Down Expand Up @@ -145,6 +145,13 @@ export type StdioSingleOption<
| StdinSingleOption<IsSync, IsExtra, IsArray>
| StdoutStderrSingleOption<IsSync, IsExtra, IsArray>;

// 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<IsSync extends boolean = boolean> =
| StdinOptionCommon<IsSync>
Expand Down
7 changes: 2 additions & 5 deletions types/transform/object-mode.d.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -10,10 +10,7 @@ export type IsObjectFd<
OptionsType extends CommonOptions = CommonOptions,
> = IsObjectStdioOption<FdStdioOption<FdNumber, OptionsType>>;

type IsObjectStdioOption<StdioOptionType extends StdioOptionCommon> = IsObjectStdioSingleOption<StdioOptionType extends readonly StdioSingleOption[]
? StdioOptionType[number]
: StdioOptionType
>;
type IsObjectStdioOption<StdioOptionType extends StdioOptionCommon> = IsObjectStdioSingleOption<StdioSingleOptionItems<StdioOptionType>>;

type IsObjectStdioSingleOption<StdioSingleOptionType extends StdioSingleOption> = StdioSingleOptionType extends {objectMode?: boolean}
? BooleanObjectMode<StdioSingleOptionType['objectMode']>
Expand Down
4 changes: 4 additions & 0 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export type Or<First extends boolean, Second extends boolean> = First extends tr
export type Unless<Condition extends boolean, ThenValue, ElseValue = never> = Condition extends true ? ElseValue : ThenValue;

export type AndUnless<Condition extends boolean, ThenValue, ElseValue = unknown> = 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<T, U> = true extends (T extends U ? true : false) ? true : false;

0 comments on commit 6cc519b

Please sign in to comment.