Skip to content

Commit

Permalink
Export TemplateExpression type (#1049)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored May 10, 2024
1 parent b8c131c commit 2d84752
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
15 changes: 10 additions & 5 deletions docs/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## 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), [`StdinOption`](api.md#optionsstdin) and [`StdoutStderrOption`](api.md#optionsstdout).
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), [`StdinOption`](api.md#optionsstdin), [`StdoutStderrOption`](api.md#optionsstdout) and [`TemplateExpression`](api.md#execacommand).

```ts
import {
Expand All @@ -19,6 +19,7 @@ import {
type Options,
type StdinOption,
type StdoutStderrOption,
type TemplateExpression,
} from 'execa';

const options: Options = {
Expand All @@ -27,9 +28,10 @@ const options: Options = {
stderr: 'pipe' satisfies StdoutStderrOption,
timeout: 1000,
};
const task: TemplateExpression = 'build';

try {
const subprocess: ResultPromise = execa(options)`npm run build`;
const subprocess: ResultPromise = execa(options)`npm run ${task}`;
const result: Result = await subprocess;
console.log(result.stdout);
} catch (error) {
Expand All @@ -41,7 +43,7 @@ try {

## Synchronous execution

Their [synchronous](#synchronous-execution) counterparts are [`SyncResult`](api.md#result), [`ExecaSyncError`](api.md#execasyncerror), [`SyncOptions`](api.md#options), [`StdinSyncOption`](api.md#optionsstdin) and [`StdoutStderrSyncOption`](api.md#optionsstdout).
Their [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).

```ts
import {
Expand All @@ -51,6 +53,7 @@ import {
type SyncOptions,
type StdinSyncOption,
type StdoutStderrSyncOption,
type TemplateExpression,
} from 'execa';

const options: SyncOptions = {
Expand All @@ -59,9 +62,10 @@ const options: SyncOptions = {
stderr: 'pipe' satisfies StdoutStderrSyncOption,
timeout: 1000,
};
const task: TemplateExpression = 'build';

try {
const result: SyncResult = execaSync(options)`npm run build`;
const result: SyncResult = execaSync(options)`npm run ${task}`;
console.log(result.stdout);
} catch (error) {
if (error instanceof ExecaSyncError) {
Expand Down Expand Up @@ -91,9 +95,10 @@ const options = {
stderr: 'pipe',
timeout: 1000,
} as const;
const task = 'build';

try {
const subprocess = execa(options)`npm run build`;
const subprocess = execa(options)`npm run ${task}`;
const result = await subprocess;
printResultStdout(result);
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type {Options, SyncOptions} from './types/arguments/options.js';
export type {Result, SyncResult} from './types/return/result.js';
export type {ResultPromise, Subprocess} from './types/subprocess/subprocess.js';
export {ExecaError, ExecaSyncError} from './types/return/final-error.js';
export type {TemplateExpression} from './types/methods/template.js';
export {execa} from './types/methods/main-async.js';
export {execaSync} from './types/methods/main-sync.js';
export {execaCommand, execaCommandSync} from './types/methods/command.js';
Expand Down
18 changes: 18 additions & 0 deletions test-d/methods/template.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {expectAssignable, expectNotAssignable} from 'tsd';
import {execa, type TemplateExpression} from '../../index.js';

const stringArray = ['foo', 'bar'] as const;
const numberArray = [1, 2] as const;

expectAssignable<TemplateExpression>('unicorns');
expectAssignable<TemplateExpression>(1);
expectAssignable<TemplateExpression>(stringArray);
expectAssignable<TemplateExpression>(numberArray);
expectAssignable<TemplateExpression>(false.toString());
expectNotAssignable<TemplateExpression>(false);

expectAssignable<TemplateExpression>(await execa`echo foo`);
expectAssignable<TemplateExpression>(await execa({reject: false})`echo foo`);
expectNotAssignable<TemplateExpression>(execa`echo foo`);
expectAssignable<TemplateExpression>([await execa`echo foo`, 'bar']);
expectNotAssignable<TemplateExpression>([execa`echo foo`, 'bar']);
4 changes: 3 additions & 1 deletion types/methods/template.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type {Result, SyncResult} from '../return/result.js';

// Values allowed inside `...${...}...` template syntax
type TemplateExpressionItem =
| string
| number
| Result
| SyncResult;

/**
Value allowed inside `${...}` when using the template string syntax.
*/
export type TemplateExpression = TemplateExpressionItem | readonly TemplateExpressionItem[];

// `...${...}...` template syntax
Expand Down

0 comments on commit 2d84752

Please sign in to comment.