Skip to content

Commit

Permalink
Simplify types (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 9, 2024
1 parent a12dba9 commit 9693cba
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 88 deletions.
38 changes: 19 additions & 19 deletions test-d/transform/object-mode.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const objectFinal = function * () {
yield {};
};

const objectTransformLinesStdoutResult = await execa('unicorns', {lines: true, stdout: {transform: objectGenerator, final: objectFinal, objectMode: true}});
const objectTransformLinesStdoutResult = await execa('unicorns', {lines: true, stdout: {transform: objectGenerator, final: objectFinal, objectMode: true} as const});
expectType<unknown[]>(objectTransformLinesStdoutResult.stdout);
expectType<[undefined, unknown[], string[]]>(objectTransformLinesStdoutResult.stdio);

Expand All @@ -38,7 +38,7 @@ const objectDuplexPropertyStdoutResult = await execa('unicorns', {stdout: duplex
expectType<unknown[]>(objectDuplexPropertyStdoutResult.stdout);
expectType<[undefined, unknown[], string]>(objectDuplexPropertyStdoutResult.stdio);

const objectTransformStdoutResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: true}});
const objectTransformStdoutResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: true} as const});
expectType<unknown[]>(objectTransformStdoutResult.stdout);
expectType<[undefined, unknown[], string]>(objectTransformStdoutResult.stdio);

Expand All @@ -54,7 +54,7 @@ const objectDuplexPropertyStderrResult = await execa('unicorns', {stderr: duplex
expectType<unknown[]>(objectDuplexPropertyStderrResult.stderr);
expectType<[undefined, string, unknown[]]>(objectDuplexPropertyStderrResult.stdio);

const objectTransformStderrResult = await execa('unicorns', {stderr: {transform: objectGenerator, final: objectFinal, objectMode: true}});
const objectTransformStderrResult = await execa('unicorns', {stderr: {transform: objectGenerator, final: objectFinal, objectMode: true} as const});
expectType<unknown[]>(objectTransformStderrResult.stderr);
expectType<[undefined, string, unknown[]]>(objectTransformStderrResult.stdio);

Expand All @@ -70,7 +70,7 @@ const objectDuplexPropertyStdioResult = await execa('unicorns', {stdio: ['pipe',
expectType<unknown[]>(objectDuplexPropertyStdioResult.stderr);
expectType<[undefined, string, unknown[]]>(objectDuplexPropertyStdioResult.stdio);

const objectTransformStdioResult = await execa('unicorns', {stdio: ['pipe', 'pipe', {transform: objectGenerator, final: objectFinal, objectMode: true}]});
const objectTransformStdioResult = await execa('unicorns', {stdio: ['pipe', 'pipe', {transform: objectGenerator, final: objectFinal, objectMode: true} as const]});
expectType<unknown[]>(objectTransformStdioResult.stderr);
expectType<[undefined, string, unknown[]]>(objectTransformStdioResult.stdio);

Expand All @@ -86,7 +86,7 @@ const singleObjectDuplexPropertyStdoutResult = await execa('unicorns', {stdout:
expectType<unknown[]>(singleObjectDuplexPropertyStdoutResult.stdout);
expectType<[undefined, unknown[], string]>(singleObjectDuplexPropertyStdoutResult.stdio);

const singleObjectTransformStdoutResult = await execa('unicorns', {stdout: [{transform: objectGenerator, final: objectFinal, objectMode: true}]});
const singleObjectTransformStdoutResult = await execa('unicorns', {stdout: [{transform: objectGenerator, final: objectFinal, objectMode: true} as const]});
expectType<unknown[]>(singleObjectTransformStdoutResult.stdout);
expectType<[undefined, unknown[], string]>(singleObjectTransformStdoutResult.stdio);

Expand All @@ -102,7 +102,7 @@ const manyObjectDuplexPropertyStdoutResult = await execa('unicorns', {stdout: [d
expectType<unknown[]>(manyObjectDuplexPropertyStdoutResult.stdout);
expectType<[undefined, unknown[], string]>(manyObjectDuplexPropertyStdoutResult.stdio);

const manyObjectTransformStdoutResult = await execa('unicorns', {stdout: [{transform: objectGenerator, final: objectFinal, objectMode: true}, {transform: objectGenerator, final: objectFinal, objectMode: true}]});
const manyObjectTransformStdoutResult = await execa('unicorns', {stdout: [{transform: objectGenerator, final: objectFinal, objectMode: true} as const, {transform: objectGenerator, final: objectFinal, objectMode: true} as const]});
expectType<unknown[]>(manyObjectTransformStdoutResult.stdout);
expectType<[undefined, unknown[], string]>(manyObjectTransformStdoutResult.stdio);

Expand All @@ -118,7 +118,7 @@ const falseObjectDuplexPropertyStdoutResult = await execa('unicorns', {stdout: d
expectType<string>(falseObjectDuplexPropertyStdoutResult.stdout);
expectType<[undefined, string, string]>(falseObjectDuplexPropertyStdoutResult.stdio);

const falseObjectTransformStdoutResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: false}});
const falseObjectTransformStdoutResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: false} as const});
expectType<string>(falseObjectTransformStdoutResult.stdout);
expectType<[undefined, string, string]>(falseObjectTransformStdoutResult.stdio);

Expand All @@ -134,7 +134,7 @@ const falseObjectDuplexPropertyStderrResult = await execa('unicorns', {stderr: d
expectType<string>(falseObjectDuplexPropertyStderrResult.stderr);
expectType<[undefined, string, string]>(falseObjectDuplexPropertyStderrResult.stdio);

const falseObjectTransformStderrResult = await execa('unicorns', {stderr: {transform: objectGenerator, final: objectFinal, objectMode: false}});
const falseObjectTransformStderrResult = await execa('unicorns', {stderr: {transform: objectGenerator, final: objectFinal, objectMode: false} as const});
expectType<string>(falseObjectTransformStderrResult.stderr);
expectType<[undefined, string, string]>(falseObjectTransformStderrResult.stdio);

Expand All @@ -150,7 +150,7 @@ const falseObjectDuplexPropertyStdioResult = await execa('unicorns', {stdio: ['p
expectType<string>(falseObjectDuplexPropertyStdioResult.stderr);
expectType<[undefined, string, string]>(falseObjectDuplexPropertyStdioResult.stdio);

const falseObjectTransformStdioResult = await execa('unicorns', {stdio: ['pipe', 'pipe', {transform: objectGenerator, final: objectFinal, objectMode: false}]});
const falseObjectTransformStdioResult = await execa('unicorns', {stdio: ['pipe', 'pipe', {transform: objectGenerator, final: objectFinal, objectMode: false} as const]});
expectType<string>(falseObjectTransformStdioResult.stderr);
expectType<[undefined, string, string]>(falseObjectTransformStdioResult.stdio);

Expand All @@ -174,50 +174,50 @@ const noObjectTransformStdoutResult = await execa('unicorns', {stdout: objectGen
expectType<string>(noObjectTransformStdoutResult.stdout);
expectType<[undefined, string, string]>(noObjectTransformStdoutResult.stdio);

const trueTrueObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: true}, stderr: {transform: objectGenerator, final: objectFinal, objectMode: true}, all: true});
const trueTrueObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: true} as const, stderr: {transform: objectGenerator, final: objectFinal, objectMode: true} as const, all: true});
expectType<unknown[]>(trueTrueObjectTransformResult.stdout);
expectType<unknown[]>(trueTrueObjectTransformResult.stderr);
expectType<unknown[]>(trueTrueObjectTransformResult.all);
expectType<[undefined, unknown[], unknown[]]>(trueTrueObjectTransformResult.stdio);

const trueFalseObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: true}, stderr: {transform: objectGenerator, final: objectFinal, objectMode: false}, all: true});
const trueFalseObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: true} as const, stderr: {transform: objectGenerator, final: objectFinal, objectMode: false} as const, all: true});
expectType<unknown[]>(trueFalseObjectTransformResult.stdout);
expectType<string>(trueFalseObjectTransformResult.stderr);
expectType<unknown[]>(trueFalseObjectTransformResult.all);
expectType<[undefined, unknown[], string]>(trueFalseObjectTransformResult.stdio);

const falseTrueObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: false}, stderr: {transform: objectGenerator, final: objectFinal, objectMode: true}, all: true});
const falseTrueObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: false} as const, stderr: {transform: objectGenerator, final: objectFinal, objectMode: true} as const, all: true});
expectType<string>(falseTrueObjectTransformResult.stdout);
expectType<unknown[]>(falseTrueObjectTransformResult.stderr);
expectType<unknown[]>(falseTrueObjectTransformResult.all);
expectType<[undefined, string, unknown[]]>(falseTrueObjectTransformResult.stdio);

const falseFalseObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: false}, stderr: {transform: objectGenerator, final: objectFinal, objectMode: false}, all: true});
const falseFalseObjectTransformResult = await execa('unicorns', {stdout: {transform: objectGenerator, final: objectFinal, objectMode: false} as const, stderr: {transform: objectGenerator, final: objectFinal, objectMode: false} as const, all: true});
expectType<string>(falseFalseObjectTransformResult.stdout);
expectType<string>(falseFalseObjectTransformResult.stderr);
expectType<string>(falseFalseObjectTransformResult.all);
expectType<[undefined, string, string]>(falseFalseObjectTransformResult.stdio);

const objectTransformStdoutError = new Error('.') as ExecaError<{stdout: {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: true}}>;
const objectTransformStdoutError = new Error('.') as ExecaError<{stdout: {transform: typeof objectGenerator; final: typeof objectFinal; readonly objectMode: true}}>;
expectType<unknown[]>(objectTransformStdoutError.stdout);
expectType<[undefined, unknown[], string]>(objectTransformStdoutError.stdio);

const objectTransformStderrError = new Error('.') as ExecaError<{stderr: {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: true}}>;
const objectTransformStderrError = new Error('.') as ExecaError<{stderr: {transform: typeof objectGenerator; final: typeof objectFinal; readonly objectMode: true}}>;
expectType<unknown[]>(objectTransformStderrError.stderr);
expectType<[undefined, string, unknown[]]>(objectTransformStderrError.stdio);

const objectTransformStdioError = new Error('.') as ExecaError<{stdio: ['pipe', 'pipe', {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: true}]}>;
const objectTransformStdioError = new Error('.') as ExecaError<{stdio: ['pipe', 'pipe', {transform: typeof objectGenerator; final: typeof objectFinal; readonly objectMode: true}]}>;
expectType<unknown[]>(objectTransformStdioError.stderr);
expectType<[undefined, string, unknown[]]>(objectTransformStdioError.stdio);

const falseObjectTransformStdoutError = new Error('.') as ExecaError<{stdout: {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: false}}>;
const falseObjectTransformStdoutError = new Error('.') as ExecaError<{stdout: {transform: typeof objectGenerator; final: typeof objectFinal; readonly objectMode: false}}>;
expectType<string>(falseObjectTransformStdoutError.stdout);
expectType<[undefined, string, string]>(falseObjectTransformStdoutError.stdio);

const falseObjectTransformStderrError = new Error('.') as ExecaError<{stderr: {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: false}}>;
const falseObjectTransformStderrError = new Error('.') as ExecaError<{stderr: {transform: typeof objectGenerator; final: typeof objectFinal; readonly objectMode: false}}>;
expectType<string>(falseObjectTransformStderrError.stderr);
expectType<[undefined, string, string]>(falseObjectTransformStderrError.stdio);

const falseObjectTransformStdioError = new Error('.') as ExecaError<{stdio: ['pipe', 'pipe', {transform: typeof objectGenerator; final: typeof objectFinal; objectMode: false}]}>;
const falseObjectTransformStdioError = new Error('.') as ExecaError<{stdio: ['pipe', 'pipe', {transform: typeof objectGenerator; final: typeof objectFinal; readonly objectMode: false}]}>;
expectType<string>(falseObjectTransformStdioError.stderr);
expectType<[undefined, string, string]>(falseObjectTransformStdioError.stdio);
10 changes: 5 additions & 5 deletions types/arguments/specific.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type {FromOption} from './fd-options.js';

// Options which can be fd-specific like `{verbose: {stdout: 'none', stderr: 'full'}}`
export type FdGenericOption<OptionType = unknown> = OptionType | GenericOptionObject<OptionType>;
export type FdGenericOption<OptionType> = OptionType | GenericOptionObject<OptionType>;

type GenericOptionObject<OptionType = unknown> = {
type GenericOptionObject<OptionType> = {
readonly [FdName in FromOption]?: OptionType
};

// Retrieve fd-specific option's value
export type FdSpecificOption<
GenericOption extends FdGenericOption,
GenericOption extends FdGenericOption<unknown>,
FdNumber extends string,
> = GenericOption extends GenericOptionObject
> = GenericOption extends GenericOptionObject<unknown>
? FdSpecificObjectOption<GenericOption, FdNumber>
: GenericOption;

type FdSpecificObjectOption<
GenericOption extends GenericOptionObject,
GenericOption extends GenericOptionObject<unknown>,
FdNumber extends string,
> = keyof GenericOption extends FromOption
? FdNumberToFromOption<FdNumber, keyof GenericOption> extends never
Expand Down
8 changes: 5 additions & 3 deletions types/methods/template.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type {CommonOptions} from '../arguments/options.js';
import type {CommonResultInstance} from '../return/result.js';

// Values allowed inside `...${...}...` template syntax
export type TemplateExpression =
type TemplateExpressionItem =
| string
| number
| CommonResultInstance
| ReadonlyArray<string | number | CommonResultInstance>;
| CommonResultInstance<boolean, CommonOptions>;

export type TemplateExpression = TemplateExpressionItem | readonly TemplateExpressionItem[];

// `...${...}...` template syntax
export type TemplateString = readonly [TemplateStringsArray, ...readonly TemplateExpression[]];
Expand Down
20 changes: 13 additions & 7 deletions types/return/final-error.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ import type {CommonOptions, Options, SyncOptions} from '../arguments/options.js'
import {CommonResult} from './result.js';

declare abstract class CommonError<
IsSync extends boolean = boolean,
OptionsType extends CommonOptions = CommonOptions,
IsSync extends boolean,
OptionsType extends CommonOptions,
> extends CommonResult<IsSync, OptionsType> {
message: NonNullable<CommonResult['message']>;
shortMessage: NonNullable<CommonResult['shortMessage']>;
originalMessage: NonNullable<CommonResult['originalMessage']>;
readonly name: NonNullable<CommonResult['name']>;
stack: NonNullable<CommonResult['stack']>;
message: CommonErrorProperty<IsSync, OptionsType, 'message'>;
shortMessage: CommonErrorProperty<IsSync, OptionsType, 'shortMessage'>;
originalMessage: CommonErrorProperty<IsSync, OptionsType, 'originalMessage'>;
readonly name: CommonErrorProperty<IsSync, OptionsType, 'name'>;
stack: CommonErrorProperty<IsSync, OptionsType, 'stack'>;
}

type CommonErrorProperty<
IsSync extends boolean,
OptionsType extends CommonOptions,
PropertyName extends keyof CommonResult<IsSync, OptionsType>,
> = NonNullable<CommonResult<IsSync, OptionsType>[PropertyName]>;

// `result.*` defined only on failure, i.e. on `error.*`
export type ErrorProperties =
| 'name'
Expand Down
4 changes: 2 additions & 2 deletions types/return/ignore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {CommonOptions} from '../arguments/options.js';
// Whether `result.stdin|stdout|stderr|all|stdio[*]` is `undefined`
export type IgnoresResultOutput<
FdNumber extends string,
OptionsType extends CommonOptions = CommonOptions,
OptionsType extends CommonOptions,
> = FdSpecificOption<OptionsType['buffer'], FdNumber> extends false
? true
: IsInputFd<FdNumber, OptionsType> extends true
Expand All @@ -17,7 +17,7 @@ export type IgnoresResultOutput<
// Whether `subprocess.stdout|stderr|all` is `undefined|null`
export type IgnoresSubprocessOutput<
FdNumber extends string,
OptionsType extends CommonOptions = CommonOptions,
OptionsType extends CommonOptions,
> = IgnoresOutput<FdNumber, FdStdioOption<FdNumber, OptionsType>>;

type IgnoresOutput<
Expand Down
12 changes: 6 additions & 6 deletions types/return/result-all.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import type {IgnoresResultOutput} from './ignore.js';
import type {ResultStdio} from './result-stdout.js';

// `result.all`
export type ResultAll<OptionsType extends CommonOptions = CommonOptions> =
export type ResultAll<OptionsType extends CommonOptions> =
ResultAllProperty<OptionsType['all'], OptionsType>;

type ResultAllProperty<
AllOption extends CommonOptions['all'] = CommonOptions['all'],
OptionsType extends CommonOptions = CommonOptions,
AllOption extends CommonOptions['all'],
OptionsType extends CommonOptions,
> = AllOption extends true
? ResultStdio<
AllMainFd<OptionsType>,
Expand All @@ -20,11 +20,11 @@ type ResultAllProperty<
>
: undefined;

type AllMainFd<OptionsType extends CommonOptions = CommonOptions> =
type AllMainFd<OptionsType extends CommonOptions> =
IgnoresResultOutput<'1', OptionsType> extends true ? '2' : '1';

type AllObjectFd<OptionsType extends CommonOptions = CommonOptions> =
type AllObjectFd<OptionsType extends CommonOptions> =
IsObjectFd<'1', OptionsType> extends true ? '1' : '2';

type AllLinesFd<OptionsType extends CommonOptions = CommonOptions> =
type AllLinesFd<OptionsType extends CommonOptions> =
FdSpecificOption<OptionsType['lines'], '1'> extends true ? '1' : '2';
4 changes: 2 additions & 2 deletions types/return/result-stdio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import type {CommonOptions} from '../arguments/options.js';
import type {ResultStdioNotAll} from './result-stdout.js';

// `result.stdio`
export type ResultStdioArray<OptionsType extends CommonOptions = CommonOptions> =
export type ResultStdioArray<OptionsType extends CommonOptions> =
MapResultStdio<StdioOptionNormalizedArray<OptionsType>, OptionsType>;

type MapResultStdio<
StdioOptionsArrayType extends StdioOptionsArray,
OptionsType extends CommonOptions = CommonOptions,
OptionsType extends CommonOptions,
> = {
-readonly [FdNumber in keyof StdioOptionsArrayType]: ResultStdioNotAll<
FdNumber extends string ? FdNumber : string,
Expand Down
6 changes: 3 additions & 3 deletions types/return/result-stdout.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import type {IgnoresResultOutput} from './ignore.js';
// `result.stdout|stderr|stdio`
export type ResultStdioNotAll<
FdNumber extends string,
OptionsType extends CommonOptions = CommonOptions,
OptionsType extends CommonOptions,
> = ResultStdio<FdNumber, FdNumber, FdNumber, OptionsType>;

// `result.stdout|stderr|stdio|all`
export type ResultStdio<
MainFdNumber extends string,
ObjectFdNumber extends string,
LinesFdNumber extends string,
OptionsType extends CommonOptions = CommonOptions,
OptionsType extends CommonOptions,
> = ResultStdioProperty<
ObjectFdNumber,
LinesFdNumber,
Expand All @@ -27,7 +27,7 @@ type ResultStdioProperty<
ObjectFdNumber extends string,
LinesFdNumber extends string,
StreamOutputIgnored extends boolean,
OptionsType extends CommonOptions = CommonOptions,
OptionsType extends CommonOptions,
> = StreamOutputIgnored extends true
? undefined
: ResultStdioItem<
Expand Down
12 changes: 6 additions & 6 deletions types/return/result.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type {ResultStdioArray} from './result-stdio.js';
import type {ResultStdioNotAll} from './result-stdout.js';

export declare abstract class CommonResult<
IsSync extends boolean = boolean,
OptionsType extends CommonOptions = CommonOptions,
IsSync extends boolean,
OptionsType extends CommonOptions,
> {
/**
The output of the subprocess on [`stdout`](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)).
Expand Down Expand Up @@ -162,13 +162,13 @@ export declare abstract class CommonResult<
}

export type CommonResultInstance<
IsSync extends boolean = boolean,
OptionsType extends CommonOptions = CommonOptions,
IsSync extends boolean,
OptionsType extends CommonOptions,
> = InstanceType<typeof CommonResult<IsSync, OptionsType>>;

type SuccessResult<
IsSync extends boolean = boolean,
OptionsType extends CommonOptions = CommonOptions,
IsSync extends boolean,
OptionsType extends CommonOptions,
> = CommonResultInstance<IsSync, OptionsType> & OmitErrorIfReject<OptionsType['reject']>;

type OmitErrorIfReject<RejectOption extends CommonOptions['reject']> = RejectOption extends false
Expand Down
Loading

0 comments on commit 9693cba

Please sign in to comment.