Refactor types (#685)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 938d4b1 · Browse files · 2 files·+70 added−37 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+70 added−37 removed
- other:+70 added−37 removed
Provenance
- Commit
ae64408- Tree
1fab77c- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
938d4b1
2 files changed+70 added−37 removed
modified:index.d.ts+60 added-37 removed
73 unmodified lines74 | undefined;75 type DefaultEncodingOption = 'utf8';76 type BufferEncodingOption = 'buffer';77 78-type TupleItem<79- Tuple extends readonly unknown[],80- Index extends string,81-> = Tuple[Index extends keyof Tuple ? Index : number];82-78 // Whether `result.stdout|stderr|all` is `undefined`, excluding the `buffer` option79 type IgnoresStreamResult<80 StreamIndex extends string,81 OptionsType extends CommonOptions = CommonOptions,82 // `result.stdin` is always `undefined`83 > = StreamIndex extends '0' ? true84 // When using `stdout|stderr: 'inherit'`, or `'ignore'`, etc. , `result.std*` is `undefined`90- : OptionsType[TupleItem<StdioOptionNames, StreamIndex>] extends NoOutputStdioOption ? true91- // Same but with `stdio: 'ignore'`92- : OptionsType['stdio'] extends NoOutputStdioOption ? true93- // Same but with `stdio: ['ignore', 'ignore', 'ignore', ...]`94- : OptionsType['stdio'] extends StdioOptionsArray95- ? TupleItem<OptionsType['stdio'], StreamIndex> extends StdioOptionsArray[number]96- ? IgnoresStdioResult<TupleItem<OptionsType['stdio'], StreamIndex>>97- : false98- : false;85+ : IgnoresNormalPropertyResult<StreamIndex, OptionsType> extends true ? true86+ // Otherwise87+ : IgnoresStdioPropertyResult<StreamIndex, OptionsType['stdio']>;88 89+type IgnoresNormalPropertyResult<90+ StreamIndex extends string,91+ OptionsType extends CommonOptions = CommonOptions,92+> = StreamIndex extends keyof StdioOptionNames93+ ? StdioOptionNames[StreamIndex] extends keyof OptionsType94+ ? OptionsType[StdioOptionNames[StreamIndex]] extends NoOutputStdioOption95+ ? true96+ : false97+ : false98+ : false;99+100 type StdioOptionNames = ['stdin', 'stdout', 'stderr'];101 102+type IgnoresStdioPropertyResult<103+ StreamIndex extends string,104+ StdioOptionType extends StdioOptions | undefined,105+ // Same but with `stdio: 'ignore'`106+> = StdioOptionType extends NoOutputStdioOption ? true107+// Same but with `stdio: ['ignore', 'ignore', 'ignore', ...]`108+ : StdioOptionType extends StdioOptionsArray109+ ? StreamIndex extends keyof StdioOptionType110+ ? StdioOptionType[StreamIndex] extends StdioOption111+ ? IgnoresStdioResult<StdioOptionType[StreamIndex]>112+ : false113+ : false114+ : false;115+116 // Whether `result.stdio[*]` is `undefined`103-type IgnoresStdioResult<104- StdioOption extends StdioOptionsArray[number],105-> = StdioOption extends NoOutputStdioOption106- ? true117+type IgnoresStdioResult<StdioOptionType extends StdioOption> =118+ StdioOptionType extends NoOutputStdioOption ? true119 // `result.stdio[3+]` is `undefined` when it is an input stream108- : StdioOption extends StdinOption109- ? StdioOption extends StdoutStderrOption110- ? false111- : true112- : false;120+ : StdioOptionType extends StdinOption121+ ? StdioOptionType extends StdoutStderrOption122 123+ ? false124+ : true125+ : false;126+127 // Whether `result.stdout|stderr|all` is `undefined`128 type IgnoresStreamOutput<129 StreamIndex extends string,130 OptionsType extends CommonOptions = CommonOptions,118-> = OptionsType extends {buffer: false} ? true119- : IgnoresStreamResult<StreamIndex, OptionsType> extends true ? true : false;131+> = HasBuffer<OptionsType> extends false132+ ? true133+ : IgnoresStreamResult<StreamIndex, OptionsType>;134 135+type HasBuffer<OptionsType extends CommonOptions> = OptionsType extends Options136+ ? HasBufferOption<OptionsType['buffer']>137+ : true;138+139+type HasBufferOption<BufferOption extends Options['buffer']> = BufferOption extends false ? false : true;140+141 // Type of `result.stdout|stderr`142 type StdioOutput<143 StreamIndex extends string,144 OptionsType extends CommonOptions = CommonOptions,125-> = IgnoresStreamOutput<StreamIndex, OptionsType> extends true145+> = StdioOutputResult<IgnoresStreamOutput<StreamIndex, OptionsType>, OptionsType>;146+147+type StdioOutputResult<148+ StreamOutputIgnored extends boolean,149+ OptionsType extends CommonOptions = CommonOptions,150+> = StreamOutputIgnored extends true151 ? undefined152 : StreamResult<OptionsType>;153 129-type StreamResult<OptionsType extends CommonOptions = CommonOptions> =130- // Default value for `encoding`, when `OptionsType` is `{}`131- unknown extends OptionsType['encoding'] ? string132- // Any value for `encoding`, when `OptionsType` is `Options`133- : EncodingOption extends OptionsType['encoding'] ? Uint8Array | string134- // `encoding: buffer`135- : OptionsType['encoding'] extends 'buffer' ? Uint8Array136- // `encoding: not buffer`137- : string;154+type StreamResult<OptionsType extends CommonOptions = CommonOptions> = StreamEncoding<OptionsType['encoding']>;155 156+type StreamEncoding<Encoding extends CommonOptions['encoding']> = Encoding extends 'buffer' ? Uint8Array : string;157+158 // Type of `result.all`159 type AllOutput<OptionsType extends Options = Options> = AllOutputProperty<OptionsType['all'], OptionsType>;160 161 type AllOutputProperty<23 unmodified lines185 186 type StricterOptions<187 WideOptions extends CommonOptions,188 StrictOptions extends CommonOptions,170-> = keyof WideOptions extends never ? {} : WideOptions & StrictOptions;189+> = WideOptions extends StrictOptions ? WideOptions : StrictOptions;190 191 type CommonOptions<IsSync extends boolean = boolean> = {192 /**193 Prefer locally installed binaries when looking for a binary to execute.459 unmodified lines653 654 type StreamUnlessIgnored<655 StreamIndex extends string,656 OptionsType extends Options = Options,638-> = IgnoresStreamResult<StreamIndex, OptionsType> extends true ? null : Readable;657+> = ChildProcessStream<IgnoresStreamResult<StreamIndex, OptionsType>>;658 659+type ChildProcessStream<StreamResultIgnored extends boolean> = StreamResultIgnored extends true660+ ? null661+ : Readable;662+663 export type ExecaChildPromise<OptionsType extends Options = Options> = {664 stdout: StreamUnlessIgnored<'1', OptionsType>;665 666 stderr: StreamUnlessIgnored<'2', OptionsType>;530 unmodified linesmodified:index.test-d.ts+10 added
19 unmodified lines20 type ExecaSyncReturnValue,21 type ExecaSyncError,22 } from './index.js';23 24+expectType<Readable | null>({} as ExecaChildProcess['stdout']);25+expectType<Readable | null>({} as ExecaChildProcess['stderr']);26+expectType<string | Uint8Array | undefined>({} as ExecaReturnValue['stdout']);27+expectType<string | Uint8Array | undefined>({} as ExecaReturnValue['stderr']);28+expectType<string | Uint8Array | undefined>({} as ExecaReturnValue['all']);29+expectType<[undefined, string | Uint8Array | undefined, string | Uint8Array | undefined]>({} as ExecaReturnValue['stdio']);30+expectType<string | Uint8Array | undefined>({} as ExecaSyncReturnValue['stdout']);31+expectType<string | Uint8Array | undefined>({} as ExecaSyncReturnValue['stderr']);32+expectType<[undefined, string | Uint8Array | undefined, string | Uint8Array | undefined]>({} as ExecaSyncReturnValue['stdio']);33+34 try {35 const execaPromise = execa('unicorns', {all: true});36 execaPromise.cancel();37 1023 unmodified lines