feat(prompts): add onCancel callback option to all prompts#544
Conversation
Add an optional onCancel callback to CommonOptions that is invoked when the user cancels a prompt (Ctrl+C or Escape). This eliminates the need for isCancel guards at every call site. Also exports the handleCancel utility for custom prompt implementations. Closes bombshell-dev#83 (partial — runtime behavior)
🦋 Changeset detectedLatest commit: 0125a25 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Convert all prompt functions to function declarations with overloads. When onCancel is provided, the return type narrows from Promise<T | symbol> to Promise<T>, eliminating the need for isCancel type guards. Also removes unused WithCancel and CancelAware types from common.ts.
- Add onCancel.test.ts with runtime cancel/submit tests for all 12 prompts - Expand type-narrowing.test.ts to cover autocomplete, autocompleteMultiselect, and path - All 12 prompts now have full runtime and compile-time coverage
Unsafely, yes: const result = await confirm({
message: "Continue?",
onCancel: () => {
console.log("do anything here that _doesn't_ process.exit");
},
});
// result is still T | Symbol at runtimei'm not sure we should go down this path yet until the issue has a resolution. my main concern is that this is mixing two different architectures:
we currently use the first one. introducing the second means we now have two ways of dealing with cancellations, which seems awkward to me. ultimately you're just trying to avoid having to type-check results, but that takes about as much code as your |
Summary
Adds an optional
onCancelcallback to all prompt functions viaCommonOptions. When the user cancels a prompt (Ctrl+C or Escape), the callback is invoked — eliminating the need forisCancelguards at every call site. WhenonCancelis provided, the return type narrows fromPromise<T | symbol>toPromise<T>.Closes #83
Motivation
Every clack user repeats this pattern:
This PR makes it:
Changes
CommonOptions— addedonCancel?: () => voidhandleCancelutility — exported from@clack/promptsfor custom prompt implementationsType narrowing
Each prompt uses function overloads so TypeScript narrows the return type when
onCancelis present:This works for all prompts:
text,confirm,select,multiselect,groupMultiselect,password,autocomplete,autocompleteMultiselect,selectKey,date,multiline,path.Design decisions
onCancelis optional; existing code works unchangedexport constarrows toexport functiondeclarationshandleCancelis exported — useful for custom prompt implementationsTest plan
pnpm run buildsucceedspnpm run typessucceedspnpm run lintsucceedspnpm run formatsucceeds@ts-expect-error)