Skip to content

Commit

Permalink
fix: workaround for getting overriding the default GeneratedTypes int…
Browse files Browse the repository at this point in the history
…erface to work in typescript 5.5
  • Loading branch information
AlessioGr committed Jun 21, 2024
1 parent 3bd3027 commit 0d307b1
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/payload/src/bin/generateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ export async function generateTypes(

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

const declare = `declare module 'payload' {\n export interface GeneratedTypes extends Config {}\n}`
const declareWithTSIgnoreError = `declare module 'payload' {\n // @ts-ignore \n export interface GeneratedTypes extends Config {}\n}`

let compiled = await compile(jsonSchema, 'Config', {
bannerComment:
'/* tslint:disable */\n/* eslint-disable */\n/**\n* This file was automatically generated by Payload.\n* DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,\n* and re-run `payload generate:types` to regenerate this file.\n*/',
Expand All @@ -34,6 +31,19 @@ export async function generateTypes(
unreachableDefinitions: true,
})

// Attempt to extract and replace the 'Config' interface content
const configInterfaceMatch = /export interface Config \{([\s\S]*?)^\}/m.exec(compiled)
if (!configInterfaceMatch) {
logger.error('Failed to extract the Config interface.')
return
}

// Add indentation
const configInterfaceContent = configInterfaceMatch[1].replace(/^/gm, ' ') // Add two spaces to the start of each line

const declare = `declare module 'payload' {\n export interface GeneratedTypes {\n${configInterfaceContent} }\n}`
const declareWithTSIgnoreError = `declare module 'payload' {\n // @ts-ignore \n export interface GeneratedTypes {\n${configInterfaceContent} }\n}`

if (config.typescript.declare !== false) {
if (config.typescript.declare?.ignoreTSError) {
compiled += `\n\n${declareWithTSIgnoreError}`
Expand Down

0 comments on commit 0d307b1

Please sign in to comment.