Skip to content
Myr

typescript.md

This file

Path

docs/typescript.md

139 lines · 3.5 KiB · Markdown

Last changed by

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

History of this file

Take it

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 1–139why, one step back
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>67# 🤓 TypeScript89## Available types1011The 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).1213```ts14import {15 execa,16 ExecaError,17 type ResultPromise,18 type Result,19 type Options,20 type StdinOption,21 type StdoutStderrOption,22} from 'execa';2324const options: Options = {25 stdin: 'inherit' satisfies StdinOption,26 stdout: 'pipe' satisfies StdoutStderrOption,27 stderr: 'pipe' satisfies StdoutStderrOption,28 timeout: 1000,29};3031try {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```4142## Synchronous execution4344Their [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).4546```ts47import {48 execaSync,49 ExecaSyncError,50 type SyncResult,51 type SyncOptions,52 type StdinSyncOption,53 type StdoutStderrSyncOption,54} from 'execa';5556const options: SyncOptions = {57 stdin: 'inherit' satisfies StdinSyncOption,58 stdout: 'pipe' satisfies StdoutStderrSyncOption,59 stderr: 'pipe' satisfies StdoutStderrSyncOption,60 timeout: 1000,61};6263try {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```7273## Type inference7475The 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.7677```ts78import {79 execa,80 ExecaError,81 type Result,82} from 'execa';8384const printResultStdout = (result: Result) => {85 console.log('Stdout', result.stdout);86};8788const options = {89 stdin: 'inherit',90 stdout: 'pipe',91 stderr: 'pipe',92 timeout: 1000,93} as const;9495try {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```105106## Troubleshooting107108### Strict unions109110Several 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:111112```ts113import {execa} from 'execa';114115// Type error: "No overload matches this call"116const spawnSubprocess = (serialization: string) => execa({serialization})`npm run build`;117118// Without `as const`, `options.serialization` is typed as `string`, not `'json'`119const options = {serialization: 'json'};120// Type error: "No overload matches this call"121await execa(options)`npm run build`;122```123124But this works:125126```ts127import {execa, type Options} from 'execa';128129const spawnSubprocess = (serialization: Options['serialization']) => execa({serialization})`npm run build`;130131const options = {serialization: 'json'} as const;132await execa(options)`npm run build`;133```134135<hr>136137[**Next**: 📔 API reference](api.md)\138[**Previous**: 🔍 Differences with Bash and zx](bash.md)\139[**Top**: Table of contents](../readme.md#documentation)