typescript.md
This file
Path
docs/typescript.md
191 lines · 6.1 KiB · Markdown
Last changed by
Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
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 types10Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
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-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).Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 12–14why, one step back1213```ts14import {Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe
line 15why, one step back15 execa as execa_,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 16–21why, one step back16 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 promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 23why, one step back23 type Message,Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
line 24why, one step back24 type VerboseObject,Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe
line 25why, one step back25 type ExecaMethod,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 26–27why, one step back26} from 'execa';27Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe
lines 28–29why, one step back28const execa: ExecaMethod = execa_({preferLocal: true});29Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 30–34why, one step back30const 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 back35 ipc: true,Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
lines 36–38why, one step back36 verbose(verboseLine: string, verboseObject: VerboseObject) {37 return verboseObject.type === 'duration' ? verboseLine : undefined;38 },Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
line 39why, one step back39};Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 40why, one step back40const task: TemplateExpression = 'build';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 41why, one step back41const message: Message = 'hello world';Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 42–43why, one step back4243try {Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 44why, one step back44 const subprocess: ResultPromise = execa(options)`npm run ${task}`;Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813
line 45why, one step back45 await subprocess.sendMessage?.(message);Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 46–56why, one step back46 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 execution56Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
line 57why, one step back57Their [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).Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 58–60why, one step back5859```ts60import {Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe
line 61why, one step back61 execaSync as execaSync_,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 62–66why, one step back62 ExecaSyncError,63 type SyncResult,64 type SyncOptions,65 type StdinSyncOption,66 type StdoutStderrSyncOption,Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 67why, one step back67 type TemplateExpression,Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
line 68why, one step back68 type SyncVerboseObject,Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe
line 69why, one step back69 type ExecaSyncMethod,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 70–71why, one step back70} from 'execa';71Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe
lines 72–73why, one step back72const execaSync: ExecaSyncMethod = execaSync_({preferLocal: true});73Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 74–78why, one step back74const 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 back79 verbose(verboseLine: string, verboseObject: SyncVerboseObject) {80 return verboseObject.type === 'duration' ? verboseLine : undefined;81 },Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
line 82why, one step back82};Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 83why, one step back83const task: TemplateExpression = 'build';Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 84–85why, one step back8485try {Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 86why, one step back86 const result: SyncResult = execaSync(options)`npm run ${task}`;Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 87–100why, one step back87 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 back101 execa as execa_,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 102–103why, one step back102 ExecaError,103 type Result,Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
line 104why, one step back104 type VerboseObject,Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
line 105why, one step back105} from 'execa';Export TypeScript types for the `execa` method (#1066)ehmickybefore adoptiona2e4dbe
lines 106–107why, one step back106107const execa = execa_({preferLocal: true});Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 108–117why, one step back108109const 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 back118 ipc: true,Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
lines 119–121why, one step back119 verbose(verboseLine: string, verboseObject: VerboseObject) {120 return verboseObject.type === 'duration' ? verboseLine : undefined;121 },Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
line 122why, one step back122} as const;Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 123why, one step back123const task = 'build';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 124why, one step back124const message = 'hello world';Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 125–126why, one step back125126try {Export `TemplateExpression` type (#1049)ehmickybefore adoption2d84752
line 127why, one step back127 const subprocess = execa(options)`npm run ${task}`;Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 128why, one step back128 await subprocess.sendMessage(message);Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 129–139why, one step back129 const result = await subprocess;130 printResultStdout(result);131} catch (error) {132 if (error instanceof ExecaError) {133 console.error(error);134 }135}136```137138## Troubleshooting139Document minimum TypeScript version (#1050)ehmickybefore adoptionde8e7da
lines 140–143why, one step back140### Supported version141142The minimum supported TypeScript version is [`5.1.6`](https://github.com/microsoft/TypeScript/releases/tag/v5.1.6).143Add ES modules to Troubleshooting TypeScript section (#1039)ehmickybefore adoptiona12dba9
lines 144–159why, one step back144### ES modules145146This 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```159Add documentation about TypeScript types (#1015)ehmickybefore adoptionf79e058
lines 160–161why, one step back160### Strict unions161Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813
line 162why, one step back162Several 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 163–191why, one step back163164```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)\190[**Previous**: 🔍 Differences with Bash and zx](bash.md)\191[**Top**: Table of contents](../readme.md#documentation)