typescript.md
This file
Path
docs/typescript.md
171 lines · 5.1 KiB · Markdown
Last changed by
Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813
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 types10Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813
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-1), [`StdinOption`](api.md#optionsstdin), [`StdoutStderrOption`](api.md#optionsstdout), [`TemplateExpression`](api.md#execacommand) and [`Message`](api.md#subprocesssendmessagemessage-sendmessageoptions).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 promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 23why, one step back23 type Message,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 24–30why, one step back24} from 'execa';2526const options: Options = {27 stdin: 'inherit' satisfies StdinOption,28 stdout: 'pipe' satisfies StdoutStderrOption,29 stderr: 'pipe' satisfies StdoutStderrOption,30 timeout: 1000,Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 31why, one step back31 ipc: true,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
line 32why, one step back32};Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 33why, one step back33const task: TemplateExpression = 'build';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 34why, one step back34const message: Message = 'hello world';Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 35–36why, one step back3536try {Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 37why, one step back37 const subprocess: ResultPromise = execa(options)`npm run ${task}`;Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813
line 38why, one step back38 await subprocess.sendMessage?.(message);Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 39–49why, one step back39 const result: Result = await subprocess;40 console.log(result.stdout);41} catch (error) {42 if (error instanceof ExecaError) {43 console.error(error);44 }45}46```4748## Synchronous execution49Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813
line 50why, one step back50Their [synchronous](#synchronous-execution) counterparts are [`SyncResult`](api.md#result), [`ExecaSyncError`](api.md#execasyncerror), [`SyncOptions`](api.md#options-1), [`StdinSyncOption`](api.md#optionsstdin), [`StdoutStderrSyncOption`](api.md#optionsstdout) and [`TemplateExpression`](api.md#execacommand).Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 51–59why, one step back5152```ts53import {54 execaSync,55 ExecaSyncError,56 type SyncResult,57 type SyncOptions,58 type StdinSyncOption,59 type StdoutStderrSyncOption,Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 60why, one step back60 type TemplateExpression,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 61–68why, one step back61} from 'execa';6263const options: SyncOptions = {64 stdin: 'inherit' satisfies StdinSyncOption,65 stdout: 'pipe' satisfies StdoutStderrSyncOption,66 stderr: 'pipe' satisfies StdoutStderrSyncOption,67 timeout: 1000,68};Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 69why, one step back69const task: TemplateExpression = 'build';Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 70–71why, one step back7071try {Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 72why, one step back72 const result: SyncResult = execaSync(options)`npm run ${task}`;Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 73–100why, one step back73 console.log(result.stdout);74} catch (error) {75 if (error instanceof ExecaSyncError) {76 console.error(error);77 }78}79```8081## Type inference8283The 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.8485```ts86import {87 execa,88 ExecaError,89 type Result,90} from 'execa';9192const printResultStdout = (result: Result) => {93 console.log('Stdout', result.stdout);94};9596const options = {97 stdin: 'inherit',98 stdout: 'pipe',99 stderr: 'pipe',100 timeout: 1000,Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 101why, one step back101 ipc: true,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
line 102why, one step back102} as const;Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 103why, one step back103const task = 'build';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 104why, one step back104const message = 'hello world';Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 105–106why, one step back105106try {Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 107why, one step back107 const subprocess = execa(options)`npm run ${task}`;Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 108why, one step back108 await subprocess.sendMessage(message);Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 109–119why, one step back109 const result = await subprocess;110 printResultStdout(result);111} catch (error) {112 if (error instanceof ExecaError) {113 console.error(error);114 }115}116```117118## Troubleshooting119Document minimum TypeScript version (#1050)ehmickybefore adoptionde8e7da
lines 120–123why, one step back120### Supported version121122The minimum supported TypeScript version is [`5.1.6`](https://github.com/microsoft/TypeScript/releases/tag/v5.1.6).123Add ES modules to Troubleshooting TypeScript section (#1039)ehmickybefore adoptiona12dba9
lines 124–139why, one step back124### ES modules125126This 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)127128Otherwise, transpilation will work, but running the transpiled file will throw the following runtime error:129130```131Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.132```133134Or:135136```137ReferenceError: exports is not defined in ES module scope138```139Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 140–141why, one step back140### Strict unions141Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813
line 142why, one step back142Several options are typed as unions of strings: [`stdin`](api.md#optionsstdin), [`stdout`](api.md#optionsstdout), [`stderr`](api.md#optionsstderr), [`encoding`](api.md#optionsencoding), [`serialization`](api.md#optionsserialization), [`verbose`](api.md#optionsverbose), [`killSignal`](api.md#optionskillsignal), [`from`](api.md#pipeoptionsfrom) and [`to`](api.md#pipeoptionsto). For example, the `serialization` option's type is `'advanced' | 'json'`, not `string`. Therefore the following example fails:Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 143–171why, one step back143144```ts145import {execa} from 'execa';146147// Type error: "No overload matches this call"148const spawnSubprocess = (serialization: string) => execa({serialization})`npm run build`;149150// Without `as const`, `options.serialization` is typed as `string`, not `'json'`151const options = {serialization: 'json'};152// Type error: "No overload matches this call"153await execa(options)`npm run build`;154```155156But this works:157158```ts159import {execa, type Options} from 'execa';160161const spawnSubprocess = (serialization: Options['serialization']) => execa({serialization})`npm run build`;162163const options = {serialization: 'json'} as const;164await execa(options)`npm run build`;165```166167<hr>168169[**Next**: 📔 API reference](api.md)\170[**Previous**: 🔍 Differences with Bash and zx](bash.md)\171[**Top**: Table of contents](../readme.md#documentation)