feat: add support for interfaceName on radio and select fields to create reusable top level types (#11277)

Adds support for `interfaceName` on radio and select fields. Adding this
property will extract your provided options into a top level type for
re-use.


![image](https://github.com/user-attachments/assets/be5c3e17-5127-4546-a778-d3aa801dec90)

Added types test to make sure assignment is consistent.
This commit is contained in:
Paul
2025-02-19 13:51:03 +00:00
committed by GitHub
parent bf103cc025
commit acead1083b
7 changed files with 103 additions and 3 deletions

View File

@@ -22,6 +22,38 @@ export default buildConfigWithDefaults({
type: 'text',
name: 'title',
},
{
name: 'selectField',
type: 'select',
required: true,
interfaceName: 'MySelectOptions',
options: [
{
label: 'Option 1',
value: 'option-1',
},
{
label: 'Option 2',
value: 'option-2',
},
],
},
{
name: 'radioField',
type: 'radio',
required: true,
interfaceName: 'MyRadioOptions',
options: [
{
label: 'Option 1',
value: 'option-1',
},
{
label: 'Option 2',
value: 'option-2',
},
],
},
],
},
{

View File

@@ -6,6 +6,16 @@
* and re-run `payload generate:types` to regenerate this file.
*/
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "MySelectOptions".
*/
export type MySelectOptions = 'option-1' | 'option-2';
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "MyRadioOptions".
*/
export type MyRadioOptions = 'option-1' | 'option-2';
/**
* Supported timezones in IANA format.
*
@@ -132,6 +142,8 @@ export interface Post {
id: string;
text?: string | null;
title?: string | null;
selectField: MySelectOptions;
radioField: MyRadioOptions;
updatedAt: string;
createdAt: string;
}
@@ -249,6 +261,8 @@ export interface PayloadMigration {
export interface PostsSelect<T extends boolean = true> {
text?: T;
title?: T;
selectField?: T;
radioField?: T;
updatedAt?: T;
createdAt?: T;
}

View File

@@ -10,7 +10,14 @@ import type {
import payload from 'payload'
import { describe, expect, test } from 'tstyche'
import type { Menu, Post, User } from './payload-types.js'
import type {
Menu,
MyRadioOptions,
MySelectOptions,
Post,
SupportedTimezones,
User,
} from './payload-types.js'
const asType = <T>() => {
return '' as T
@@ -124,4 +131,18 @@ describe('Types testing', () => {
}>()
})
})
describe('generated types', () => {
test('has SupportedTimezones', () => {
expect<SupportedTimezones>().type.toBeAssignableTo<string>()
})
test('has global generated options interface based on select field', () => {
expect(asType<Post['selectField']>()).type.toBe<MySelectOptions>()
})
test('has global generated options interface based on radio field', () => {
expect(asType<Post['radioField']>()).type.toBe<MyRadioOptions>()
})
})
})