Skip to content

Commit 4e4e5a6

Browse files
committed
Enforce exhaustiveness here too.
1 parent ed12be4 commit 4e4e5a6

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/lib/messages.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CommentTableRow } from '@/entrypoints/background'
2-
import type { CommentEvent } from './enhancer'
2+
import type { CommentEvent, CommentEventType } from './enhancer'
33

44
// Message handler response types
55
export const CLOSE_MESSAGE_PORT = false as const // No response will be sent
@@ -29,12 +29,24 @@ export interface GetTableRowsResponse {
2929
rows: CommentTableRow[]
3030
}
3131

32+
// Exhaustive list of valid comment event types - TypeScript will error if CommentEventType changes
33+
const COMMENT_EVENT_TYPES = {
34+
DESTROYED: true,
35+
ENHANCED: true,
36+
LOST_FOCUS: true,
37+
} as const satisfies Record<CommentEventType, true>
38+
39+
// Helper function to check if a string is a valid CommentEventType
40+
function isValidCommentEventType(type: string): type is CommentEventType {
41+
return type in COMMENT_EVENT_TYPES
42+
}
43+
3244
// Type guard functions
3345
export function isContentToBackgroundMessage(message: any): message is ContentToBackgroundMessage {
3446
return (
3547
message &&
3648
typeof message.type === 'string' &&
37-
(message.type === 'ENHANCED' || message.type === 'DESTROYED') &&
49+
isValidCommentEventType(message.type) &&
3850
message.spot
3951
)
4052
}

0 commit comments

Comments
 (0)