Skip to content
Myr

typescript.md

This file

Path

docs/typescript.md

191 lines · 6.1 KiB · Markdown

Last changed by

Add documentation about nano-spawn (#1157)ehmickybefore adoptioneb3cfba

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

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), [`VerboseObject`](api.md#verbose-object), [`ExecaMethod`](api.md#execaoptions), [`ExecaNodeMethod`](api.md#execanodeoptions) and [`ExecaScriptMethod`](api.md#options).

100% test coverage (#1018)ehmickybefore adoption9f65b86

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_,

100% test coverage (#1018)ehmickybefore adoption9f65b86

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,

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

line 24why, one step back
24 type VerboseObject,

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

line 25why, one step back
25 type ExecaMethod,

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

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

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 36–38why, one step back
36 verbose(verboseLine: string, verboseObject: VerboseObject) {
37 return verboseObject.type === 'duration' ? verboseLine : undefined;38 },

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 39why, one step back
39};

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 42–43why, one step back
42
43try {

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 46–56why, one step back
46 const result: Result = await subprocess;
47 console.log(result.stdout);48} catch (error) {49 if (error instanceof ExecaError) {50 console.error(error);51 }52}53```5455## Synchronous execution56

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 58–60why, one step back
58
59```ts60import {

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

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 62–66why, one step back
62 ExecaSyncError,
63 type SyncResult,64 type SyncOptions,65 type StdinSyncOption,66 type StdoutStderrSyncOption,

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

line 67why, one step back
67 type TemplateExpression,

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

line 68why, one step back
68 type SyncVerboseObject,

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

line 69why, one step back
69 type ExecaSyncMethod,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 70–71why, one step back
70} from 'execa';
71

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

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 74–78why, one step back
74const options: SyncOptions = {
75 stdin: 'inherit' satisfies StdinSyncOption,76 stdout: 'pipe' satisfies StdoutStderrSyncOption,77 stderr: 'pipe' satisfies StdoutStderrSyncOption,78 timeout: 1000,

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 79–81why, one step back
79 verbose(verboseLine: string, verboseObject: SyncVerboseObject) {
80 return verboseObject.type === 'duration' ? verboseLine : undefined;81 },

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 82why, one step back
82};

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 84–85why, one step back
84
85try {

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 87–100why, one step back
87 console.log(result.stdout);
88} catch (error) {89 if (error instanceof ExecaSyncError) {90 console.error(error);91 }92}93```9495## Type inference9697The 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.9899```ts100import {

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

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 102–103why, one step back
102 ExecaError,
103 type Result,

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

line 104why, one step back
104 type VerboseObject,

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 105why, one step back
105} from 'execa';

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

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 108–117why, one step back
108
109const printResultStdout = (result: Result) => {110 console.log('Stdout', result.stdout);111};112113const options = {114 stdin: 'inherit',115 stdout: 'pipe',116 stderr: 'pipe',117 timeout: 1000,

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 119–121why, one step back
119 verbose(verboseLine: string, verboseObject: VerboseObject) {
120 return verboseObject.type === 'duration' ? verboseLine : undefined;121 },

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 125–126why, one step back
125
126try {

Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 129–139why, one step back
129 const result = await subprocess;
130 printResultStdout(result);131} catch (error) {132 if (error instanceof ExecaError) {133 console.error(error);134 }135}136```137138## Troubleshooting139

Document minimum TypeScript version (#1050)ehmickybefore adoptionde8e7da

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

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

lines 144–159why, one step back
144### ES modules
145146This 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)147148Otherwise, transpilation will work, but running the transpiled file will throw the following runtime error:149150```151Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.152```153154Or:155156```157ReferenceError: exports is not defined in ES module scope158```159

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 160–161why, one step back
160### Strict unions
161

Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813

line 162why, one step back
162Several 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:

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 163–189why, one step back
163
164```ts165import {execa} from 'execa';166167// Type error: "No overload matches this call"168const spawnSubprocess = (serialization: string) => execa({serialization})`npm run build`;169170// Without `as const`, `options.serialization` is typed as `string`, not `'json'`171const options = {serialization: 'json'};172// Type error: "No overload matches this call"173await execa(options)`npm run build`;174```175176But this works:177178```ts179import {execa, type Options} from 'execa';180181const spawnSubprocess = (serialization: Options['serialization']) => execa({serialization})`npm run build`;182183const options = {serialization: 'json'} as const;184await execa(options)`npm run build`;185```186187<hr>188189[**Next**: 📔 API reference](api.md)\

Add documentation about nano-spawn (#1157)ehmickybefore adoptioneb3cfba

line 190why, one step back
190[**Previous**: 🐭 Small packages](small.md)\

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 191why, one step back
191[**Top**: Table of contents](../readme.md#documentation)