Fix TypeScript compatibility with PayloadCMS generated types

Resolves: TS2344: Type Email does not satisfy the constraint BaseEmailData
- Add null support to BaseEmailData interface for all optional fields
- Update parseAndValidateEmails to handle null values
- Update sendEmail validation to properly check for null values
- Maintain compatibility with PayloadCMS generated types that include null

Generated Email types like cc?: string[] | null | undefined now work correctly.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-13 21:03:21 +02:00
parent 1af54c6573
commit e1800f5a6e
2 changed files with 14 additions and 13 deletions

View File

@@ -2,15 +2,16 @@ import { Payload } from 'payload'
import { getMailing, renderTemplate, parseAndValidateEmails } from './utils/helpers.js'
// Base type for email data that all emails must have
// Compatible with PayloadCMS generated types that include null
export interface BaseEmailData {
to: string | string[]
cc?: string | string[]
bcc?: string | string[]
subject?: string
html?: string
text?: string
scheduledAt?: string | Date
priority?: number
cc?: string | string[] | null
bcc?: string | string[] | null
subject?: string | null
html?: string | null
text?: string | null
scheduledAt?: string | Date | null
priority?: number | null
[key: string]: any
}
@@ -78,18 +79,18 @@ export const sendEmail = async <T extends BaseEmailData = BaseEmailData>(
throw new Error('Field "to" is required for sending emails')
}
if (!emailData.subject || !emailData.html) {
if (!emailData.subject || emailData.subject === null || !emailData.html || emailData.html === null) {
throw new Error('Fields "subject" and "html" are required when not using a template')
}
// Process email addresses using shared validation
// Process email addresses using shared validation (handle null values)
if (emailData.to) {
emailData.to = parseAndValidateEmails(emailData.to as string | string[])
}
if (emailData.cc) {
if (emailData.cc && emailData.cc !== null) {
emailData.cc = parseAndValidateEmails(emailData.cc as string | string[])
}
if (emailData.bcc) {
if (emailData.bcc && emailData.bcc !== null) {
emailData.bcc = parseAndValidateEmails(emailData.bcc as string | string[])
}