typescript.md
This file
Path
docs/typescript.md
164 lines · 4.5 KiB · Markdown
Last changed by
Document minimum TypeScript version (#1050)ehmickybefore adoptionde8e7da
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## Troubleshooting112Document minimum TypeScript version (#1050)ehmickybefore adoptionde8e7da
lines 113–116why, one step back113### Supported version114115The minimum supported TypeScript version is [`5.1.6`](https://github.com/microsoft/TypeScript/releases/tag/v5.1.6).116Add ES modules to Troubleshooting TypeScript section (#1039)ehmickybefore adoptiona12dba9
lines 117–132why, one step back117### ES modules118119This 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)120121Otherwise, transpilation will work, but running the transpiled file will throw the following runtime error:122123```124Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.125```126127Or:128129```130ReferenceError: exports is not defined in ES module scope131```132Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 133–164why, one step back133### Strict unions134135Several 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:136137```ts138import {execa} from 'execa';139140// Type error: "No overload matches this call"141const spawnSubprocess = (serialization: string) => execa({serialization})`npm run build`;142143// Without `as const`, `options.serialization` is typed as `string`, not `'json'`144const options = {serialization: 'json'};145// Type error: "No overload matches this call"146await execa(options)`npm run build`;147```148149But this works:150151```ts152import {execa, type Options} from 'execa';153154const spawnSubprocess = (serialization: Options['serialization']) => execa({serialization})`npm run build`;155156const options = {serialization: 'json'} as const;157await execa(options)`npm run build`;158```159160<hr>161162[**Next**: 📔 API reference](api.md)\163[**Previous**: 🔍 Differences with Bash and zx](bash.md)\164[**Top**: Table of contents](../readme.md#documentation)