Skip to content

Commit 7309d47

Browse files
authored
feat!: type auto-generation (#6657)
Types are now auto-generated by default. You can opt-out of this behavior by setting: ```ts buildConfig({ // Rest of config typescript: { autoGenerate: false }, }) ```
1 parent 45e8683 commit 7309d47

File tree

81 files changed

+743
-39
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+743
-39
lines changed

next.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default withBundleAnalyzer(
3333
'.js': ['.ts', '.tsx', '.js', '.jsx'],
3434
'.mjs': ['.mts', '.mjs'],
3535
}
36+
3637
return webpackConfig
3738
},
3839
}),

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"husky": "^8.0.3",
125125
"jest": "29.7.0",
126126
"jest-environment-jsdom": "29.7.0",
127+
"json-schema-to-typescript": "11.0.3",
127128
"lint-staged": "^14.0.1",
128129
"minimist": "1.2.8",
129130
"mongodb-memory-server": "^9.0",

packages/next/src/utilities/getPayloadHMR.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { GeneratedTypes, Payload } from 'payload'
22
import type { InitOptions, SanitizedConfig } from 'payload/config'
33

44
import { BasePayload } from 'payload'
5+
import { generateTypes } from 'payload/bin'
56
import WebSocket from 'ws'
67

78
let cached: {
@@ -36,6 +37,11 @@ export const reload = async (config: SanitizedConfig, payload: Payload): Promise
3637

3738
// TODO: support HMR for other props in the future (see payload/src/index init()) hat may change on Payload singleton
3839

40+
// Generate types
41+
if (config.typescript.autoGenerate !== false) {
42+
void generateTypes(config, { log: false })
43+
}
44+
3945
await payload.db.init()
4046
if (payload.db.connect) {
4147
await payload.db.connect({ hotReload: true })

packages/next/src/withPayload.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const withPayload = (nextConfig = {}) => {
6868
'pino',
6969
'pino-pretty',
7070
'graphql',
71+
'json-schema-to-typescript',
7172
],
7273
webpack: (webpackConfig, webpackOptions) => {
7374
const incomingWebpackConfig =
@@ -83,6 +84,7 @@ export const withPayload = (nextConfig = {}) => {
8384
'drizzle-kit/payload',
8485
'sharp',
8586
'libsql',
87+
'json-schema-to-typescript',
8688
],
8789
ignoreWarnings: [
8890
...(incomingWebpackConfig?.ignoreWarnings || []),

packages/next/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const componentWebpackConfig = {
1818
'payload/config',
1919
'react-image-crop',
2020
'payload/operations',
21+
'payload/bin',
2122
],
2223
mode: 'production',
2324
module: {

packages/payload/src/bin/generateTypes.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import type { SanitizedConfig } from '../config/types.js'
66
import { configToJSONSchema } from '../utilities/configToJSONSchema.js'
77
import Logger from '../utilities/logger.js'
88

9-
export async function generateTypes(config: SanitizedConfig): Promise<void> {
9+
export async function generateTypes(
10+
config: SanitizedConfig,
11+
options?: { log: boolean },
12+
): Promise<void> {
1013
const logger = Logger()
1114
const outputFile = process.env.PAYLOAD_TS_OUTPUT_PATH || config.typescript.outputFile
1215

13-
logger.info('Compiling TS types for Collections and Globals...')
16+
const shouldLog = options?.log ?? true
17+
18+
if (shouldLog) logger.info('Compiling TS types for Collections and Globals...')
1419

1520
const jsonSchema = configToJSONSchema(config, config.db.defaultIDType)
1621

@@ -37,5 +42,5 @@ export async function generateTypes(config: SanitizedConfig): Promise<void> {
3742
}
3843
}
3944
fs.writeFileSync(outputFile, compiled)
40-
logger.info(`Types written to ${outputFile}`)
45+
if (shouldLog) logger.info(`Types written to ${outputFile}`)
4146
}

packages/payload/src/config/defaults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const defaults: Omit<Config, 'db' | 'editor' | 'secret'> = {
4949
serverURL: '',
5050
telemetry: true,
5151
typescript: {
52+
autoGenerate: true,
5253
outputFile: `${typeof process?.cwd === 'function' ? process.cwd() : ''}/payload-types.ts`,
5354
},
5455
upload: {},

packages/payload/src/config/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export default joi.object({
189189
sharp: joi.any(),
190190
telemetry: joi.boolean(),
191191
typescript: joi.object({
192+
autoGenerate: joi.boolean(),
192193
declare: joi.alternatives().try(joi.boolean(), joi.object({ ignoreTSError: joi.boolean() })),
193194
outputFile: joi.string(),
194195
}),

packages/payload/src/config/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,12 @@ export type Config = {
719719
telemetry?: boolean
720720
/** Control how typescript interfaces are generated from your collections. */
721721
typescript?: {
722+
/**
723+
* Automatically generate types during development
724+
* @default true
725+
*/
726+
autoGenerate?: boolean
727+
722728
/** Disable declare block in generated types file */
723729
declare?:
724730
| {
@@ -732,6 +738,7 @@ export type Config = {
732738
ignoreTSError?: boolean
733739
}
734740
| false
741+
735742
/** Filename to write the generated types to */
736743
outputFile?: string
737744
}

packages/payload/src/exports/bin.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* WARNING: This file contains exports that can only be safely used on the server.
3+
*/
4+
5+
export { generateTypes } from '../bin/generateTypes.js'

0 commit comments

Comments
 (0)