Export TypeScript types for the `execa` method (#1066)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 8572eb1 · Browse files · 7 files·+85 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+85 added−14 removed
- other:+85 added−14 removed
Provenance
- Commit
a2e4dbe- Tree
1b4464b- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
8572eb1
7 files changed+85 added−14 removed
modified:docs/typescript.md+13 added-5 removed
6 unmodified lines7891011111213141515161718192021222324252627282930313216 unmodified lines495051525053545556545758596061626364656667686970717216 unmodified lines89909192879394959697989910010110210376 unmodified lines6 unmodified lines# 🤓 TypeScript
## Available types
The 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) and [`Message`](api.md#subprocesssendmessagemessage-sendmessageoptions).The 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).
```tsimport { execa, execa as execa_, ExecaError, type ResultPromise, type Result, type Options, type StdinOption, type StdoutStderrOption, type TemplateExpression, type Message, type ExecaMethod,} from 'execa';
const execa: ExecaMethod = execa_({preferLocal: true});
const options: Options = { stdin: 'inherit' satisfies StdinOption, stdout: 'pipe' satisfies StdoutStderrOption, stderr: 'pipe' satisfies StdoutStderrOption,16 unmodified lines```
## Synchronous execution
Their [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) and [`TemplateExpression`](api.md#execacommand).Their [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).
```tsimport { execaSync, execaSync as execaSync_, ExecaSyncError, type SyncResult, type SyncOptions, type StdinSyncOption, type StdoutStderrSyncOption, type TemplateExpression, type ExecaSyncMethod,} from 'execa';
const execaSync: ExecaSyncMethod = execaSync_({preferLocal: true});
const options: SyncOptions = { stdin: 'inherit' satisfies StdinSyncOption, stdout: 'pipe' satisfies StdoutStderrSyncOption, stderr: 'pipe' satisfies StdoutStderrSyncOption,16 unmodified linesThe 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.
```tsimport { execa, execa as execa_, ExecaError, type Result,} from 'execa';
const execa = execa_({preferLocal: true});
const printResultStdout = (result: Result) => { console.log('Stdout', result.stdout);};
76 unmodified linesadded:test-d/methods/list.test-d.ts+63 added
1+import {expectAssignable} from 'tsd';2+import {3+ type ExecaMethod,4+ type ExecaSyncMethod,5+ type ExecaNodeMethod,6+ type ExecaScriptMethod,7+ type ExecaScriptSyncMethod,8+ execa,9+ execaSync,10+ execaNode,11+ $,12+} from '../../index.js';13+14+const options = {preferLocal: true} as const;15+const secondOptions = {node: true} as const;16+17+expectAssignable<ExecaMethod>(execa);18+expectAssignable<ExecaMethod>(execa({}));19+expectAssignable<ExecaMethod>(execa({})({}));20+expectAssignable<ExecaMethod>(execa(options));21+expectAssignable<ExecaMethod>(execa(options)(secondOptions));22+expectAssignable<ExecaMethod>(execa(options)({}));23+expectAssignable<ExecaMethod>(execa({})(options));24+25+expectAssignable<ExecaSyncMethod>(execaSync);26+expectAssignable<ExecaSyncMethod>(execaSync({}));27+expectAssignable<ExecaSyncMethod>(execaSync({})({}));28+expectAssignable<ExecaSyncMethod>(execaSync(options));29+expectAssignable<ExecaSyncMethod>(execaSync(options)(secondOptions));30+expectAssignable<ExecaSyncMethod>(execaSync(options)({}));31+expectAssignable<ExecaSyncMethod>(execaSync({})(options));32+33+expectAssignable<ExecaNodeMethod>(execaNode);34+expectAssignable<ExecaNodeMethod>(execaNode({}));35+expectAssignable<ExecaNodeMethod>(execaNode({})({}));36+expectAssignable<ExecaNodeMethod>(execaNode(options));37+expectAssignable<ExecaNodeMethod>(execaNode(options)(secondOptions));38+expectAssignable<ExecaNodeMethod>(execaNode(options)({}));39+expectAssignable<ExecaNodeMethod>(execaNode({})(options));40+41+expectAssignable<ExecaScriptMethod>($);42+expectAssignable<ExecaScriptMethod>($({}));43+expectAssignable<ExecaScriptMethod>($({})({}));44+expectAssignable<ExecaScriptMethod>($(options));45+expectAssignable<ExecaScriptMethod>($(options)(secondOptions));46+expectAssignable<ExecaScriptMethod>($(options)({}));47+expectAssignable<ExecaScriptMethod>($({})(options));48+49+expectAssignable<ExecaScriptSyncMethod>($.sync);50+expectAssignable<ExecaScriptSyncMethod>($.sync({}));51+expectAssignable<ExecaScriptSyncMethod>($.sync({})({}));52+expectAssignable<ExecaScriptSyncMethod>($.sync(options));53+expectAssignable<ExecaScriptSyncMethod>($.sync(options)(secondOptions));54+expectAssignable<ExecaScriptSyncMethod>($.sync(options)({}));55+expectAssignable<ExecaScriptSyncMethod>($.sync({})(options));56+57+expectAssignable<ExecaScriptSyncMethod>($.s);58+expectAssignable<ExecaScriptSyncMethod>($.s({}));59+expectAssignable<ExecaScriptSyncMethod>($.s({})({}));60+expectAssignable<ExecaScriptSyncMethod>($.s(options));61+expectAssignable<ExecaScriptSyncMethod>($.s(options)(secondOptions));62+expectAssignable<ExecaScriptSyncMethod>($.s(options)({}));63+expectAssignable<ExecaScriptSyncMethod>($.s({})(options));modified:types/methods/main-async.d.ts+1 added-1 removed
322 unmodified lines323 324 /**325 `execa()` method either exported by Execa, or bound using `execa(options)`.326 */327-type ExecaMethod<OptionsType extends Options> =327+export type ExecaMethod<OptionsType extends Options = Options> =328 & ExecaBind<OptionsType>329 & ExecaTemplate<OptionsType>330 & ExecaArrayLong<OptionsType>331 & ExecaArrayShort<OptionsType>;20 unmodified linesmodified:types/methods/main-sync.d.ts+1 added-1 removed
30 unmodified lines31 export declare const execaSync: ExecaSyncMethod<{}>;32 33 // For the moment, we purposely do not export `ExecaSyncMethod` and `ExecaScriptSyncMethod`.34 // This is because synchronous invocation is discouraged.35-type ExecaSyncMethod<OptionsType extends SyncOptions> =35+export type ExecaSyncMethod<OptionsType extends SyncOptions = SyncOptions> =36 & ExecaSyncBind<OptionsType>37 & ExecaSyncTemplate<OptionsType>38 & ExecaSyncArrayLong<OptionsType>39 & ExecaSyncArrayShort<OptionsType>;20 unmodified linesmodified:types/methods/node.d.ts+1 added-1 removed
33 unmodified lines34 35 /**36 `execaNode()` method either exported by Execa, or bound using `execaNode(options)`.37 */38-type ExecaNodeMethod<OptionsType extends Options> =38+export type ExecaNodeMethod<OptionsType extends Options = Options> =39 & ExecaNodeBind<OptionsType>40 & ExecaNodeTemplate<OptionsType>41 & ExecaNodeArrayLong<OptionsType>42 & ExecaNodeArrayShort<OptionsType>;20 unmodified linesmodified:types/methods/script.d.ts+2 added-2 removed
53 unmodified lines54 55 /**56 `$()` method either exported by Execa, or bound using `$(options)`.57 */58-type ExecaScriptMethod<OptionsType extends CommonOptions> =58+export type ExecaScriptMethod<OptionsType extends CommonOptions = CommonOptions> =59 & ExecaScriptBind<OptionsType>60 & ExecaScriptTemplate<OptionsType>61 & ExecaScriptArrayLong<OptionsType>62 & ExecaScriptArrayShort<OptionsType>24 unmodified lines87 // See https://github.com/microsoft/TypeScript/issues/5876588 /**89 `$.sync()` method either exported by Execa, or bound using `$.sync(options)`.90 */91-type ExecaScriptSyncMethod<OptionsType extends CommonOptions> =91+export type ExecaScriptSyncMethod<OptionsType extends CommonOptions = CommonOptions> =92 & ExecaScriptSyncBind<OptionsType>93 & ExecaScriptSyncTemplate<OptionsType>94 & ExecaScriptSyncArrayLong<OptionsType>95 & ExecaScriptSyncArrayShort<OptionsType>;20 unmodified linesmodified:index.d.ts+4 added-4 removed
9 unmodified lines10 export type {Result, SyncResult} from './types/return/result.js';11 export type {ResultPromise, Subprocess} from './types/subprocess/subprocess.js';12 export {ExecaError, ExecaSyncError} from './types/return/final-error.js';13 14-export {execa} from './types/methods/main-async.js';15-export {execaSync} from './types/methods/main-sync.js';14+export {execa, type ExecaMethod} from './types/methods/main-async.js';15+export {execaSync, type ExecaSyncMethod} from './types/methods/main-sync.js';16 export {execaCommand, execaCommandSync, parseCommandString} from './types/methods/command.js';17-export {$} from './types/methods/script.js';18-export {execaNode} from './types/methods/node.js';17+export {$, type ExecaScriptMethod, type ExecaScriptSyncMethod} from './types/methods/script.js';18+export {execaNode, type ExecaNodeMethod} from './types/methods/node.js';19 20 export {21 sendMessage,22 getOneMessage,4 unmodified lines