Skip to content

Commit 9d7bd16

Browse files
contributorwerererer
authored andcommitted
createNote: better typing without cast and never type
1 parent facbc65 commit 9d7bd16

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

apps/client/src/services/note_create.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ type PromptingRule = {
5858
*
5959
* Combine with `&` to ensure valid logical combinations.
6060
*/
61-
export type CreateNoteOpts = {
62-
target: "into" | "after" | "before" | "inbox";
61+
type CreateNoteBase = {
6362
isProtected?: boolean;
6463
saveSelection?: boolean;
6564
title?: string | null;
@@ -77,19 +76,22 @@ export type CreateNoteOpts = {
7776
* Serves as a base for "into", "before", and "after" variants,
7877
* sharing common URL-related fields.
7978
*/
80-
export type CreateNoteWithUrlOpts = CreateNoteOpts & {
79+
export type CreateNoteWithUrlOpts = CreateNoteBase & {
80+
target: "into" | "after" | "before";
8181
// `Url` may refer to either parentNotePath or parentNoteId.
8282
// The vocabulary is inspired by existing function getNoteIdFromUrl.
8383
parentNoteUrl: string;
8484

8585
// Disambiguates the position for cloned notes.
8686
targetBranchId?: string;
87-
}
87+
};
8888

89-
type NeverDefineParentNoteUrlRule = {
89+
export type CreateNoteIntoInboxOpts = CreateNoteBase & {
90+
target: "inbox";
9091
parentNoteUrl?: never;
9192
};
92-
export type CreateNoteIntoInboxOpts = CreateNoteOpts & NeverDefineParentNoteUrlRule;
93+
94+
export type CreateNoteOpts = CreateNoteWithUrlOpts | CreateNoteIntoInboxOpts;
9395

9496
interface Response {
9597
// TODO: Deduplicate with server once we have client/server architecture.
@@ -110,24 +112,20 @@ async function createNote(
110112

111113
// handle prompts centrally to write once fix for all
112114
if (options.promptForType) {
113-
let maybeResolvedOptions = await promptForType(options);
115+
const maybeResolvedOptions = await promptForType(options);
114116
if (!maybeResolvedOptions) {
115-
return {
116-
note: null, branch: undefined
117-
};
117+
return { note: null, branch: undefined };
118118
}
119119

120120
resolvedOptions = maybeResolvedOptions;
121121
}
122122

123123
if (resolvedOptions.target === "inbox") {
124-
return createNoteIntoInbox(resolvedOptions as CreateNoteIntoInboxOpts);
124+
return createNoteIntoInbox(resolvedOptions);
125125
}
126126

127-
return createNoteWithUrl(
128-
resolvedOptions.target,
129-
resolvedOptions as CreateNoteWithUrlOpts
130-
);
127+
// Only "into" | "before" | "after" reach here
128+
return createNoteWithUrl(resolvedOptions.target, resolvedOptions);
131129
}
132130

133131
async function promptForType(

0 commit comments

Comments
 (0)