Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6cc519b

Browse files
authored
Fix complexity bug with types (#1037)
1 parent 398d090 commit 6cc519b

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

types/stdio/direction.d.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {CommonOptions} from '../arguments/options.js';
2-
import type {StdinOptionCommon, StdoutStderrOptionCommon, StdioOptionCommon} from './type.js';
2+
import type {Intersects} from '../utils.js';
3+
import type {StdioSingleOptionItems, InputStdioOption} from './type.js';
34
import type {FdStdioArrayOption} from './option.js';
45

56
// Whether `result.stdio[FdNumber]` is an input stream
@@ -8,11 +9,4 @@ export type IsInputFd<
89
OptionsType extends CommonOptions = CommonOptions,
910
> = FdNumber extends '0'
1011
? true
11-
: IsInputDescriptor<FdStdioArrayOption<FdNumber, OptionsType>>;
12-
13-
// Whether `result.stdio[3+]` is an input stream
14-
type IsInputDescriptor<StdioOptionType extends StdioOptionCommon> = StdioOptionType extends StdinOptionCommon
15-
? StdioOptionType extends StdoutStderrOptionCommon
16-
? false
17-
: true
18-
: false;
12+
: Intersects<StdioSingleOptionItems<FdStdioArrayOption<FdNumber, OptionsType>>, InputStdioOption>;

types/stdio/type.d.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ type ProcessStdinFd = {readonly fd?: 0};
6767
type ProcessStdoutStderrFd = {readonly fd?: 1 | 2};
6868

6969
// Values available only in `options.stdin|stdio`
70-
type InputStdioOption<
71-
IsSync extends boolean,
72-
IsExtra extends boolean,
73-
IsArray extends boolean,
70+
export type InputStdioOption<
71+
IsSync extends boolean = boolean,
72+
IsExtra extends boolean = boolean,
73+
IsArray extends boolean = boolean,
7474
> =
7575
| 0
7676
| Unless<And<IsSync, IsExtra>, Uint8Array | IterableObject<IsArray>>
@@ -145,6 +145,13 @@ export type StdioSingleOption<
145145
| StdinSingleOption<IsSync, IsExtra, IsArray>
146146
| StdoutStderrSingleOption<IsSync, IsExtra, IsArray>;
147147

148+
// Get `options.stdin|stdout|stderr|stdio` items if it is an array, else keep as is
149+
export type StdioSingleOptionItems<
150+
StdioOptionType extends StdioOptionCommon,
151+
> = StdioOptionType extends readonly StdioSingleOption[]
152+
? StdioOptionType[number]
153+
: StdioOptionType;
154+
148155
// `options.stdin|stdout|stderr|stdio`
149156
export type StdioOptionCommon<IsSync extends boolean = boolean> =
150157
| StdinOptionCommon<IsSync>

types/transform/object-mode.d.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {StdioSingleOption, StdioOptionCommon} from '../stdio/type.js';
1+
import type {StdioSingleOption, StdioOptionCommon, StdioSingleOptionItems} from '../stdio/type.js';
22
import type {FdStdioOption} from '../stdio/option.js';
33
import type {CommonOptions} from '../arguments/options.js';
44
import type {DuplexTransform} from './normalize.js';
@@ -10,10 +10,7 @@ export type IsObjectFd<
1010
OptionsType extends CommonOptions = CommonOptions,
1111
> = IsObjectStdioOption<FdStdioOption<FdNumber, OptionsType>>;
1212

13-
type IsObjectStdioOption<StdioOptionType extends StdioOptionCommon> = IsObjectStdioSingleOption<StdioOptionType extends readonly StdioSingleOption[]
14-
? StdioOptionType[number]
15-
: StdioOptionType
16-
>;
13+
type IsObjectStdioOption<StdioOptionType extends StdioOptionCommon> = IsObjectStdioSingleOption<StdioSingleOptionItems<StdioOptionType>>;
1714

1815
type IsObjectStdioSingleOption<StdioSingleOptionType extends StdioSingleOption> = StdioSingleOptionType extends {objectMode?: boolean}
1916
? BooleanObjectMode<StdioSingleOptionType['objectMode']>

types/utils.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ export type Or<First extends boolean, Second extends boolean> = First extends tr
77
export type Unless<Condition extends boolean, ThenValue, ElseValue = never> = Condition extends true ? ElseValue : ThenValue;
88

99
export type AndUnless<Condition extends boolean, ThenValue, ElseValue = unknown> = Condition extends true ? ElseValue : ThenValue;
10+
11+
// Whether any of T's union element is the same as one of U's union element.
12+
// `&` does not work here.
13+
export type Intersects<T, U> = true extends (T extends U ? true : false) ? true : false;

0 commit comments

Comments
 (0)