createNote: better typing without cast and never type

This commit is contained in:
contributor 2025-10-28 23:28:02 +02:00 committed by Jakob Schlanstedt
parent 3438f1103d
commit 09c8a778f5

View File

@ -58,8 +58,7 @@ type PromptingRule = {
* *
* Combine with `&` to ensure valid logical combinations. * Combine with `&` to ensure valid logical combinations.
*/ */
export type CreateNoteOpts = { type CreateNoteBase = {
target: "into" | "after" | "before" | "inbox";
isProtected?: boolean; isProtected?: boolean;
saveSelection?: boolean; saveSelection?: boolean;
title?: string | null; title?: string | null;
@ -77,19 +76,22 @@ export type CreateNoteOpts = {
* Serves as a base for "into", "before", and "after" variants, * Serves as a base for "into", "before", and "after" variants,
* sharing common URL-related fields. * sharing common URL-related fields.
*/ */
export type CreateNoteWithUrlOpts = CreateNoteOpts & { export type CreateNoteWithUrlOpts = CreateNoteBase & {
target: "into" | "after" | "before";
// `Url` may refer to either parentNotePath or parentNoteId. // `Url` may refer to either parentNotePath or parentNoteId.
// The vocabulary is inspired by existing function getNoteIdFromUrl. // The vocabulary is inspired by existing function getNoteIdFromUrl.
parentNoteUrl: string; parentNoteUrl: string;
// Disambiguates the position for cloned notes. // Disambiguates the position for cloned notes.
targetBranchId?: string; targetBranchId?: string;
} };
type NeverDefineParentNoteUrlRule = { export type CreateNoteIntoInboxOpts = CreateNoteBase & {
target: "inbox";
parentNoteUrl?: never; parentNoteUrl?: never;
}; };
export type CreateNoteIntoInboxOpts = CreateNoteOpts & NeverDefineParentNoteUrlRule;
export type CreateNoteOpts = CreateNoteWithUrlOpts | CreateNoteIntoInboxOpts;
interface Response { interface Response {
// TODO: Deduplicate with server once we have client/server architecture. // TODO: Deduplicate with server once we have client/server architecture.
@ -110,24 +112,20 @@ async function createNote(
// handle prompts centrally to write once fix for all // handle prompts centrally to write once fix for all
if (options.promptForType) { if (options.promptForType) {
let maybeResolvedOptions = await promptForType(options); const maybeResolvedOptions = await promptForType(options);
if (!maybeResolvedOptions) { if (!maybeResolvedOptions) {
return { return { note: null, branch: undefined };
note: null, branch: undefined
};
} }
resolvedOptions = maybeResolvedOptions; resolvedOptions = maybeResolvedOptions;
} }
if (resolvedOptions.target === "inbox") { if (resolvedOptions.target === "inbox") {
return createNoteIntoInbox(resolvedOptions as CreateNoteIntoInboxOpts); return createNoteIntoInbox(resolvedOptions);
} }
return createNoteWithUrl( // Only "into" | "before" | "after" reach here
resolvedOptions.target, return createNoteWithUrl(resolvedOptions.target, resolvedOptions);
resolvedOptions as CreateNoteWithUrlOpts
);
} }
async function promptForType( async function promptForType(