Add documentation about TypeScript types (#1015)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 55175e8 · Browse files · 4 files·+150 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+150 added−3 removed
- other:+150 added−3 removed
Provenance
- Commit
f79e058- Tree
f88e4d8- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
55175e8
4 files changed+150 added−3 removed
modified:docs/api.md+9 added-2 removed
97 unmodified lines98 [More info.](escaping.md#user-defined-input)99 100 ## Return value101 102-_Type:_ `ResultPromise`102+_TypeScript:_ [`ResultPromise`](typescript.md)\103+_Type:_ `Promise<object> | Subprocess`104 105 The return value of all [asynchronous methods](#methods) is both:106 - the [subprocess](#subprocess).107 - a `Promise` either resolving with its successful [`result`](#result), or rejecting with its [`error`](#execaerror).1 unmodified line109 [More info.](execution.md#subprocess)110 111 ## Subprocess112 112-_Type:_ `Subprocess`113+_TypeScript:_ [`Subprocess`](typescript.md)114 115 [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with the following methods and properties.116 117 ### subprocess\[Symbol.asyncIterator\]()259 unmodified lines377 [More info.](streams.md#converting-a-subprocess-to-a-stream)378 379 ## Result380 381+_TypeScript:_ [`Result`](typescript.md) or [`SyncResult`](typescript.md\382 _Type:_ `object`383 384 [Result](execution.md#result) of a subprocess successful execution.385 221 unmodified lines607 [More info.](termination.md#signal-name-and-description)608 609 ## Options610 611+_TypeScript:_ [`Options`](typescript.md) or [`SyncOptions`](typescript.md\612 _Type:_ `object`613 614 This lists all options for [`execa()`](#execafile-arguments-options) and the [other methods](#methods).615 116 unmodified lines732 [More info.](input.md#file-input)733 734 ### options.stdin735 736+_TypeScript:_ [`StdinOption`](typescript.md) or [`StdinSyncOption`](typescript.md)\737 _Type:_ `string | number | stream.Readable | ReadableStream | TransformStream | URL | {file: string} | Uint8Array | Iterable<string | Uint8Array | unknown> | AsyncIterable<string | Uint8Array | unknown> | GeneratorFunction<string | Uint8Array | unknown> | AsyncGeneratorFunction<string | Uint8Array | unknown> | {transform: GeneratorFunction | AsyncGeneratorFunction | Duplex | TransformStream}` (or a tuple of those types)\738 _Default:_ `'inherit'` with [`$`](#file-arguments-options), `'pipe'` otherwise739 740 How to setup the subprocess' [standard input](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). This can be [`'pipe'`](streams.md#manual-streaming), [`'overlapped'`](windows.md#asynchronous-io), [`'ignore`](input.md#ignore-input), [`'inherit'`](input.md#terminal-input), a [file descriptor integer](input.md#terminal-input), a [Node.js `Readable` stream](streams.md#input), a web [`ReadableStream`](streams.md#web-streams), a [`{ file: 'path' }` object](input.md#file-input), a [file URL](input.md#file-input), an [`Iterable`](streams.md#iterables-as-input) (including an [array of strings](input.md#string-input)), an [`AsyncIterable`](streams.md#iterables-as-input), an [`Uint8Array`](binary.md#binary-input), a [generator function](transform.md), a [`Duplex`](transform.md#duplextransform-streams) or a web [`TransformStream`](transform.md#duplextransform-streams).3 unmodified lines744 More info on [available values](input.md), [streaming](streams.md) and [transforms](transform.md).745 746 ### options.stdout747 748+_TypeScript:_ [`StdoutStderrOption`](typescript.md) or [`StdoutStderrSyncOption`](typescript.md)\749 _Type:_ `string | number | stream.Writable | WritableStream | TransformStream | URL | {file: string} | GeneratorFunction<string | Uint8Array | unknown> | AsyncGeneratorFunction<string | Uint8Array | unknown> | {transform: GeneratorFunction | AsyncGeneratorFunction | Duplex | TransformStream}` (or a tuple of those types)\750 _Default:_ `pipe`751 752 How to setup the subprocess' [standard output](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). This can be [`'pipe'`](output.md#stdout-and-stderr), [`'overlapped'`](windows.md#asynchronous-io), [`'ignore`](output.md#ignore-output), [`'inherit'`](output.md#terminal-output), a [file descriptor integer](output.md#terminal-output), a [Node.js `Writable` stream](streams.md#output), a web [`WritableStream`](streams.md#web-streams), a [`{ file: 'path' }` object](output.md#file-output), a [file URL](output.md#file-output), a [generator function](transform.md), a [`Duplex`](transform.md#duplextransform-streams) or a web [`TransformStream`](transform.md#duplextransform-streams).3 unmodified lines756 More info on [available values](output.md), [streaming](streams.md) and [transforms](transform.md).757 758 ### options.stderr759 760+_TypeScript:_ [`StdoutStderrOption`](typescript.md) or [`StdoutStderrSyncOption`](typescript.md)\761 _Type:_ `string | number | stream.Writable | WritableStream | TransformStream | URL | {file: string} | GeneratorFunction<string | Uint8Array | unknown> | AsyncGeneratorFunction<string | Uint8Array | unknown> | {transform: GeneratorFunction | AsyncGeneratorFunction | Duplex | TransformStream}` (or a tuple of those types)\762 _Default:_ `pipe`763 764 How to setup the subprocess' [standard error](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). This can be [`'pipe'`](output.md#stdout-and-stderr), [`'overlapped'`](windows.md#asynchronous-io), [`'ignore`](output.md#ignore-output), [`'inherit'`](output.md#terminal-output), a [file descriptor integer](output.md#terminal-output), a [Node.js `Writable` stream](streams.md#output), a web [`WritableStream`](streams.md#web-streams), a [`{ file: 'path' }` object](output.md#file-output), a [file URL](output.md#file-output), a [generator function](transform.md), a [`Duplex`](transform.md#duplextransform-streams) or a web [`TransformStream`](transform.md#duplextransform-streams).3 unmodified lines768 More info on [available values](output.md), [streaming](streams.md) and [transforms](transform.md).769 770 ### options.stdio771 772+_TypeScript:_ [`Options['stdio']`](typescript.md) or [`SyncOptions['stdio']`](typescript.md)\773 _Type:_ `string | Array<string | number | stream.Readable | stream.Writable | ReadableStream | WritableStream | TransformStream | URL | {file: string} | Uint8Array | Iterable<string> | Iterable<Uint8Array> | Iterable<unknown> | AsyncIterable<string | Uint8Array | unknown> | GeneratorFunction<string | Uint8Array | unknown> | AsyncGeneratorFunction<string | Uint8Array | unknown> | {transform: GeneratorFunction | AsyncGeneratorFunction | Duplex | TransformStream}>` (or a tuple of those types)\774 _Default:_ `pipe`775 776 Like the [`stdin`](#optionsstdin), [`stdout`](#optionsstdout) and [`stderr`](#optionsstderr) options but for all [file descriptors](https://en.wikipedia.org/wiki/File_descriptor) at once. For example, `{stdio: ['ignore', 'pipe', 'pipe']}` is the same as `{stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}`.276 unmodified linesmodified:docs/bash.md+1 added-1 removed
887 unmodified lines888 [More info.](termination.md#inter-process-termination)889 890 <hr>891 892-[**Next**: 📔 API reference](api.md)\892+[**Next**: 🤓 TypeScript](typescript.md)\893 [**Previous**: 📎 Windows](windows.md)\894 [**Top**: Table of contents](../readme.md#documentation)added:docs/typescript.md+139 added
1+<picture>2+ <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg">3+ <img alt="execa logo" src="../media/logo.svg" width="400">4+</picture>5+<br>6+7+# 🤓 TypeScript8+9+## Available types10+11+The following types can be imported: [`ResultPromise`](api.md#return-value), [`Subprocess`](api.md#subprocess), [`Result`](api.md#result), [`ExecaError`](api.md#execaerror), [`Options`](api.md#options), [`StdinOption`](api.md#optionsstdin) and [`StdoutStderrOption`](api.md#optionsstdout).12+13+```ts14+import {15+ execa,16+ ExecaError,17+ type ResultPromise,18+ type Result,19+ type Options,20+ type StdinOption,21+ type StdoutStderrOption,22+} from 'execa';23+24+const options: Options = {25+ stdin: 'inherit' satisfies StdinOption,26+ stdout: 'pipe' satisfies StdoutStderrOption,27+ stderr: 'pipe' satisfies StdoutStderrOption,28+ timeout: 1000,29+};30+31+try {32+ const subprocess: ResultPromise = execa(options)`npm run build`;33+ const result: Result = await subprocess;34+ console.log(result.stdout);35+} catch (error) {36+ if (error instanceof ExecaError) {37+ console.error(error);38+ }39+}40+```41+42+## Synchronous execution43+44+Their [synchronous](#synchronous-execution) counterparts are [`SyncResult`](api.md#result), [`ExecaSyncError`](api.md#execasyncerror), [`SyncOptions`](api.md#options), [`StdinSyncOption`](api.md#optionsstdin) and [`StdoutStderrSyncOption`](api.md#optionsstdout).45+46+```ts47+import {48+ execaSync,49+ ExecaSyncError,50+ type SyncResult,51+ type SyncOptions,52+ type StdinSyncOption,53+ type StdoutStderrSyncOption,54+} from 'execa';55+56+const options: SyncOptions = {57+ stdin: 'inherit' satisfies StdinSyncOption,58+ stdout: 'pipe' satisfies StdoutStderrSyncOption,59+ stderr: 'pipe' satisfies StdoutStderrSyncOption,60+ timeout: 1000,61+};62+63+try {64+ const result: SyncResult = execaSync(options)`npm run build`;65+ console.log(result.stdout);66+} catch (error) {67+ if (error instanceof ExecaSyncError) {68+ console.error(error);69+ }70+}71+```72+73+## Type inference74+75+The above examples demonstrate those types. However, types are automatically inferred. Therefore, explicit types are only needed when defining functions that take those values as parameters.76+77+```ts78+import {79+ execa,80+ ExecaError,81+ type Result,82+} from 'execa';83+84+const printResultStdout = (result: Result) => {85+ console.log('Stdout', result.stdout);86+};87+88+const options = {89+ stdin: 'inherit',90+ stdout: 'pipe',91+ stderr: 'pipe',92+ timeout: 1000,93+} as const;94+95+try {96+ const subprocess = execa(options)`npm run build`;97+ const result = await subprocess;98+ printResultStdout(result);99+} catch (error) {100+ if (error instanceof ExecaError) {101+ console.error(error);102+ }103+}104+```105+106+## Troubleshooting107+108+### Strict unions109+110+Several options are typed as unions. For example, the [`serialization`](api.md#optionsserialization) option's type is `'advanced' | 'json'`, not `string`. Therefore the following example fails:111+112+```ts113+import {execa} from 'execa';114+115+// Type error: "No overload matches this call"116+const spawnSubprocess = (serialization: string) => execa({serialization})`npm run build`;117+118+// Without `as const`, `options.serialization` is typed as `string`, not `'json'`119+const options = {serialization: 'json'};120+// Type error: "No overload matches this call"121+await execa(options)`npm run build`;122+```123+124+But this works:125+126+```ts127+import {execa, type Options} from 'execa';128+129+const spawnSubprocess = (serialization: Options['serialization']) => execa({serialization})`npm run build`;130+131+const options = {serialization: 'json'} as const;132+await execa(options)`npm run build`;133+```134+135+<hr>136+137+[**Next**: 📔 API reference](api.md)\138+[**Previous**: 🔍 Differences with Bash and zx](bash.md)\139+[**Top**: Table of contents](../readme.md#documentation)modified:readme.md+1 added
93 unmodified lines94 - 📞 [Inter-process communication](docs/ipc.md)95 - 🐛 [Debugging](docs/debugging.md)96 - 📎 [Windows](docs/windows.md)97 - 🔍 [Difference with Bash and zx](docs/bash.md)98+- 🤓 [TypeScript](docs/typescript.md)99 - 📔 [API reference](docs/api.md)100 101 ## Examples102 212 unmodified lines