Skip to content
Myr

typescript.md

This file

Path

docs/typescript.md

179 lines · 5.6 KiB · Markdown

Last changed by

Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe

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

Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe

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-1), [`StdinOption`](api.md#optionsstdin), [`StdoutStderrOption`](api.md#optionsstdout), [`TemplateExpression`](api.md#execacommand), [`Message`](api.md#subprocesssendmessagemessage-sendmessageoptions), [`ExecaMethod`](api.md#execaoptions), [`ExecaNodeMethod`](api.md#execanodeoptions) and [`ExecaScriptMethod`](api.md#options).

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 12–14why, one step back
12
13```ts14import {

Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe

line 15why, one step back
15 execa as execa_,

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 16–21why, one step back
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,

Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe

line 24why, one step back
24 type ExecaMethod,

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 25–26why, one step back
25} from 'execa';
26

Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe

lines 27–28why, one step back
27const execa: ExecaMethod = execa_({preferLocal: true});
28

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 29–33why, one step back
29const options: Options = {
30 stdin: 'inherit' satisfies StdinOption,31 stdout: 'pipe' satisfies StdoutStderrOption,32 stderr: 'pipe' satisfies StdoutStderrOption,33 timeout: 1000,

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 34why, one step back
34 ipc: true,

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

line 35why, one step back
35};

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 38–39why, one step back
38
39try {

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813

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

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 42–52why, one step back
42 const result: Result = await subprocess;
43 console.log(result.stdout);44} catch (error) {45 if (error instanceof ExecaError) {46 console.error(error);47 }48}49```5051## Synchronous execution52

Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe

line 53why, one step back
53Their [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), [`TemplateExpression`](api.md#execacommand), [`ExecaSyncMethod`](api.md#execasyncoptions) and [`ExecaScriptSyncMethod`](api.md#syncoptions).

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 54–56why, one step back
54
55```ts56import {

Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe

line 57why, one step back
57 execaSync as execaSync_,

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 58–62why, one step back
58 ExecaSyncError,
59 type SyncResult,60 type SyncOptions,61 type StdinSyncOption,62 type StdoutStderrSyncOption,

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

line 63why, one step back
63 type TemplateExpression,

Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe

line 64why, one step back
64 type ExecaSyncMethod,

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 65–66why, one step back
65} from 'execa';
66

Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe

lines 67–68why, one step back
67const execaSync: ExecaSyncMethod = execaSync_({preferLocal: true});
68

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 69–74why, one step back
69const options: SyncOptions = {
70 stdin: 'inherit' satisfies StdinSyncOption,71 stdout: 'pipe' satisfies StdoutStderrSyncOption,72 stderr: 'pipe' satisfies StdoutStderrSyncOption,73 timeout: 1000,74};

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 76–77why, one step back
76
77try {

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 79–92why, one step back
79 console.log(result.stdout);
80} catch (error) {81 if (error instanceof ExecaSyncError) {82 console.error(error);83 }84}85```8687## Type inference8889The 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.9091```ts92import {

Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe

line 93why, one step back
93 execa as execa_,

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 94–96why, one step back
94 ExecaError,
95 type Result,96} from 'execa';

Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe

lines 97–98why, one step back
97
98const execa = execa_({preferLocal: true});

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 99–108why, one step back
99
100const printResultStdout = (result: Result) => {101 console.log('Stdout', result.stdout);102};103104const options = {105 stdin: 'inherit',106 stdout: 'pipe',107 stderr: 'pipe',108 timeout: 1000,

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 109why, one step back
109 ipc: true,

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

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

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 113–114why, one step back
113
114try {

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 117–127why, one step back
117 const result = await subprocess;
118 printResultStdout(result);119} catch (error) {120 if (error instanceof ExecaError) {121 console.error(error);122 }123}124```125126## Troubleshooting127

Document minimum TypeScript version (#1050)ehmickybefore adoptionde8e7da

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

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

lines 132–147why, one step back
132### ES modules
133134This 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)135136Otherwise, transpilation will work, but running the transpiled file will throw the following runtime error:137138```139Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.140```141142Or:143144```145ReferenceError: exports is not defined in ES module scope146```147

Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058

lines 148–149why, one step back
148### Strict unions
149

Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813

line 150why, one step back
150Several 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 151–179why, one step back
151
152```ts153import {execa} from 'execa';154155// Type error: "No overload matches this call"156const spawnSubprocess = (serialization: string) => execa({serialization})`npm run build`;157158// Without `as const`, `options.serialization` is typed as `string`, not `'json'`159const options = {serialization: 'json'};160// Type error: "No overload matches this call"161await execa(options)`npm run build`;162```163164But this works:165166```ts167import {execa, type Options} from 'execa';168169const spawnSubprocess = (serialization: Options['serialization']) => execa({serialization})`npm run build`;170171const options = {serialization: 'json'} as const;172await execa(options)`npm run build`;173```174175<hr>176177[**Next**: 📔 API reference](api.md)\178[**Previous**: 🔍 Differences with Bash and zx](bash.md)\179[**Top**: Table of contents](../readme.md#documentation)