Skip to content
Myr

typescript.md

This file

Path

docs/typescript.md

171 lines · 4.8 KiB · Markdown

Last changed by

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

History of this file

Take it

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 1–10why, 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 types10

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 11why, one step back
11The 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), [`TemplateExpression`](api.md#execacommand) and [`Message`](api.md#subprocesssendmessagemessage).

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 12–21why, one step back
12
13```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 back
22 type TemplateExpression,

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 23why, one step back
23 type Message,

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 24–30why, one step back
24} 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 back
31 ipc: true,

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

line 32why, one step back
32};

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

line 33why, one step back
33const task: TemplateExpression = 'build';

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 34why, one step back
34const message: Message = 'hello world';

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 35–36why, one step back
35
36try {

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

line 37why, one step back
37 const subprocess: ResultPromise = execa(options)`npm run ${task}`;

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 38why, one step back
38 await subprocess.sendMessage(message);

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 39–49why, one step back
39 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 execution49

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

line 50why, one step back
50Their [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 51–59why, one step back
51
52```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 back
60 type TemplateExpression,

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 61–68why, one step back
61} 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 back
69const task: TemplateExpression = 'build';

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 70–71why, one step back
70
71try {

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

line 72why, one step back
72 const result: SyncResult = execaSync(options)`npm run ${task}`;

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 73–100why, one step back
73 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 back
101 ipc: true,

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

line 102why, one step back
102} as const;

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

line 103why, one step back
103const task = 'build';

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 104why, one step back
104const message = 'hello world';

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 105–106why, one step back
105
106try {

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

line 107why, one step back
107 const subprocess = execa(options)`npm run ${task}`;

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 108why, one step back
108 await subprocess.sendMessage(message);

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 109–119why, one step back
109 const result = await subprocess;
110 printResultStdout(result);111} catch (error) {112 if (error instanceof ExecaError) {113 console.error(error);114 }115}116```117118## Troubleshooting119

Document minimum TypeScript version (#1050)ehmickybefore adoptionde8e7da

lines 120–123why, one step back
120### Supported version
121122The minimum supported TypeScript version is [`5.1.6`](https://github.com/microsoft/TypeScript/releases/tag/v5.1.6).123

Add ES modules to Troubleshooting TypeScript section (#1039)ehmickybefore adoptiona12dba9

lines 124–139why, one step back
124### ES modules
125126This 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```139

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 140–171why, one step back
140### Strict unions
141142Several 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:143144```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)