typescript.md
This file
Path
docs/typescript.md
155 lines · 4.2 KiB · Markdown
Last changed by
Add ES modules to Troubleshooting TypeScript section (#1039)ehmickybefore adoptiona12dba9
History of this fileTake it
Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 1–107why, 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 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## Troubleshooting107Add ES modules to Troubleshooting TypeScript section (#1039)ehmickybefore adoptiona12dba9
lines 108–123why, one step back108### ES modules109110This 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)111112Otherwise, transpilation will work, but running the transpiled file will throw the following runtime error:113114```115Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.116```117118Or:119120```121ReferenceError: exports is not defined in ES module scope122```123Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 124–155why, one step back124### Strict unions125126Several 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:127128```ts129import {execa} from 'execa';130131// Type error: "No overload matches this call"132const spawnSubprocess = (serialization: string) => execa({serialization})`npm run build`;133134// Without `as const`, `options.serialization` is typed as `string`, not `'json'`135const options = {serialization: 'json'};136// Type error: "No overload matches this call"137await execa(options)`npm run build`;138```139140But this works:141142```ts143import {execa, type Options} from 'execa';144145const spawnSubprocess = (serialization: Options['serialization']) => execa({serialization})`npm run build`;146147const options = {serialization: 'json'} as const;148await execa(options)`npm run build`;149```150151<hr>152153[**Next**: 📔 API reference](api.md)\154[**Previous**: 🔍 Differences with Bash and zx](bash.md)\155[**Top**: Table of contents](../readme.md#documentation)