fix: types for RenderField fields prop (#13162)

Fixes #7799 

Fixes a type issue where all fields in RenderFields['fields'] admin
properties were being marked as required since we were using `Pick`.
Adds a helper type to allow extracting properties with correct
optionality.
This commit is contained in:
Jarrod Flesch
2025-07-15 09:12:33 -04:00
committed by GitHub
parent 277448d9c0
commit 5f019533d8
2 changed files with 13 additions and 6 deletions

View File

@@ -142,6 +142,7 @@ import type {
JsonObject,
Operation,
PayloadRequest,
PickPreserveOptional,
Where,
} from '../../types/index.js'
import type {
@@ -632,8 +633,8 @@ export type TextField = {
Omit<FieldBase, 'validate'>
export type TextFieldClient = {
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
admin?: AdminClient & Pick<TextField['admin'], 'autoComplete' | 'placeholder' | 'rtl'>
admin?: AdminClient &
PickPreserveOptional<NonNullable<TextField['admin']>, 'autoComplete' | 'placeholder' | 'rtl'>
} & FieldBaseClient &
Pick<TextField, 'hasMany' | 'maxLength' | 'maxRows' | 'minLength' | 'minRows' | 'type'>
@@ -653,8 +654,8 @@ export type EmailField = {
} & Omit<FieldBase, 'validate'>
export type EmailFieldClient = {
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
admin?: AdminClient & Pick<EmailField['admin'], 'autoComplete' | 'placeholder'>
admin?: AdminClient &
PickPreserveOptional<NonNullable<EmailField['admin']>, 'autoComplete' | 'placeholder'>
} & FieldBaseClient &
Pick<EmailField, 'type'>
@@ -677,8 +678,8 @@ export type TextareaField = {
} & Omit<FieldBase, 'validate'>
export type TextareaFieldClient = {
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
admin?: AdminClient & Pick<TextareaField['admin'], 'placeholder' | 'rows' | 'rtl'>
admin?: AdminClient &
PickPreserveOptional<NonNullable<TextareaField['admin']>, 'placeholder' | 'rows' | 'rtl'>
} & FieldBaseClient &
Pick<TextareaField, 'maxLength' | 'minLength' | 'type'>

View File

@@ -1,5 +1,6 @@
import type { I18n, TFunction } from '@payloadcms/translations'
import type DataLoader from 'dataloader'
import type { OptionalKeys, RequiredKeys } from 'ts-essentials'
import type { URL } from 'url'
import type {
@@ -262,3 +263,8 @@ export type TransformGlobalWithSelect<
export type PopulateType = Partial<TypedCollectionSelect>
export type ResolvedFilterOptions = { [collection: string]: Where }
export type PickPreserveOptional<T, K extends keyof T> = Partial<
Pick<T, Extract<K, OptionalKeys<T>>>
> &
Pick<T, Extract<K, RequiredKeys<T>>>