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' import { getMailing, renderTemplate, parseAndValidateEmails } from './utils/helpers.js'
// Base type for email data that all emails must have // Base type for email data that all emails must have
// Compatible with PayloadCMS generated types that include null
export interface BaseEmailData { export interface BaseEmailData {
to: string | string[] to: string | string[]
cc?: string | string[] cc?: string | string[] | null
bcc?: string | string[] bcc?: string | string[] | null
subject?: string subject?: string | null
html?: string html?: string | null
text?: string text?: string | null
scheduledAt?: string | Date scheduledAt?: string | Date | null
priority?: number priority?: number | null
[key: string]: any [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') 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') 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) { if (emailData.to) {
emailData.to = parseAndValidateEmails(emailData.to as string | string[]) 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[]) 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[]) emailData.bcc = parseAndValidateEmails(emailData.bcc as string | string[])
} }

View File

@@ -5,8 +5,8 @@ import { TemplateVariables } from '../types/index.js'
* Parse and validate email addresses * Parse and validate email addresses
* @internal * @internal
*/ */
export const parseAndValidateEmails = (emails: string | string[] | undefined): string[] | undefined => { export const parseAndValidateEmails = (emails: string | string[] | null | undefined): string[] | undefined => {
if (!emails) return undefined if (!emails || emails === null) return undefined
let emailList: string[] let emailList: string[]
if (Array.isArray(emails)) { if (Array.isArray(emails)) {