typescript.md
This file
Path
docs/typescript.md
160 lines · 4.4 KiB · Markdown
Last changed by
Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
History of this fileTake it
Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 1–10why, one step back1<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 types10Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 11why, one step back11The 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), [`StdoutStderrOption`](api.md#optionsstdout) and [`TemplateExpression`](api.md#execacommand).Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 12–21why, one step back1213```ts14import {15 execa,16 ExecaError,17 type ResultPromise,18 type Result,19 type Options,20 type StdinOption,21 type StdoutStderrOption,Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 22why, one step back22 type TemplateExpression,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 23–30why, one step back23} from 'execa';2425const options: Options = {26 stdin: 'inherit' satisfies StdinOption,27 stdout: 'pipe' satisfies StdoutStderrOption,28 stderr: 'pipe' satisfies StdoutStderrOption,29 timeout: 1000,30};Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 31why, one step back31const task: TemplateExpression = 'build';Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 32–33why, one step back3233try {Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 34why, one step back34 const subprocess: ResultPromise = execa(options)`npm run ${task}`;Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 35–45why, one step back35 const result: Result = await subprocess;36 console.log(result.stdout);37} catch (error) {38 if (error instanceof ExecaError) {39 console.error(error);40 }41}42```4344## Synchronous execution45Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 46why, one step back46Their [synchronous](#synchronous-execution) counterparts are [`SyncResult`](api.md#result), [`ExecaSyncError`](api.md#execasyncerror), [`SyncOptions`](api.md#options), [`StdinSyncOption`](api.md#optionsstdin), [`StdoutStderrSyncOption`](api.md#optionsstdout) and [`TemplateExpression`](api.md#execacommand).Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 47–55why, one step back4748```ts49import {50 execaSync,51 ExecaSyncError,52 type SyncResult,53 type SyncOptions,54 type StdinSyncOption,55 type StdoutStderrSyncOption,Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 56why, one step back56 type TemplateExpression,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 57–64why, one step back57} from 'execa';5859const options: SyncOptions = {60 stdin: 'inherit' satisfies StdinSyncOption,61 stdout: 'pipe' satisfies StdoutStderrSyncOption,62 stderr: 'pipe' satisfies StdoutStderrSyncOption,63 timeout: 1000,64};Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 65why, one step back65const task: TemplateExpression = 'build';Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 66–67why, one step back6667try {Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 68why, one step back68 const result: SyncResult = execaSync(options)`npm run ${task}`;Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 69–97why, one step back69 console.log(result.stdout);70} catch (error) {71 if (error instanceof ExecaSyncError) {72 console.error(error);73 }74}75```7677## Type inference7879The 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.8081```ts82import {83 execa,84 ExecaError,85 type Result,86} from 'execa';8788const printResultStdout = (result: Result) => {89 console.log('Stdout', result.stdout);90};9192const options = {93 stdin: 'inherit',94 stdout: 'pipe',95 stderr: 'pipe',96 timeout: 1000,97} as const;Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 98why, one step back98const task = 'build';Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 99–100why, one step back99100try {Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 101why, one step back101 const subprocess = execa(options)`npm run ${task}`;Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 102–112why, one step back102 const result = await subprocess;103 printResultStdout(result);104} catch (error) {105 if (error instanceof ExecaError) {106 console.error(error);107 }108}109```110111## Troubleshooting112Add ES modules to Troubleshooting TypeScript section (#1039)ehmickybefore adoptiona12dba9
lines 113–128why, one step back113### ES modules114115This package uses pure ES modules. Therefore the TypeScript's `--module` compiler option must be set to [`nodenext`](https://www.typescriptlang.org/docs/handbook/modules/reference.html#node16-nodenext) or [`preserve`](https://www.typescriptlang.org/docs/handbook/modules/reference.html#preserve). [More info.](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)116117Otherwise, transpilation will work, but running the transpiled file will throw the following runtime error:118119```120Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.121```122123Or:124125```126ReferenceError: exports is not defined in ES module scope127```128Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 129–160why, one step back129### Strict unions130131Several 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:132133```ts134import {execa} from 'execa';135136// Type error: "No overload matches this call"137const spawnSubprocess = (serialization: string) => execa({serialization})`npm run build`;138139// Without `as const`, `options.serialization` is typed as `string`, not `'json'`140const options = {serialization: 'json'};141// Type error: "No overload matches this call"142await execa(options)`npm run build`;143```144145But this works:146147```ts148import {execa, type Options} from 'execa';149150const spawnSubprocess = (serialization: Options['serialization']) => execa({serialization})`npm run build`;151152const options = {serialization: 'json'} as const;153await execa(options)`npm run build`;154```155156<hr>157158[**Next**: 📔 API reference](api.md)\159[**Previous**: 🔍 Differences with Bash and zx](bash.md)\160[**Top**: Table of contents](../readme.md#documentation)