feat: add ability to opt out of type gen declare statement (#3765)

* feat: add ability to opt out of type gen declare statement

* chore: docs wording
This commit is contained in:
Elliot DeNolf
2023-10-19 12:44:28 -04:00
committed by GitHub
parent 13cabf129e
commit 5ff0846b6c
4 changed files with 35 additions and 5 deletions

View File

@@ -18,6 +18,32 @@ payload generate:types
You can run this command whenever you need to regenerate your types, and then you can use these types in your Payload code directly.
### Disable declare statement
By default, `generate:types` will add a `declare` statement to your types file, which automatically enables type inference within Payload.
If you are using your `payload-types.ts` file in other repos, though, it might be better to disable this `declare` statement, so that you don't get any TS errors in projects that use your Payload types, but do not have Payload installed.
```ts
// payload.config.ts
{
// ...
typescript: {
declare: false, // defaults to true if not set
},
}
```
If you do disable the `declare` pattern, you'll need to manually add a `declare` statement to your code in order for Payload types to be recognized. Here's an example showing how to declare your types in your `payload.config.ts` file:
```ts
import { Config } from './payload-types'
declare module 'payload' {
export interface GeneratedTypes extends Config {}
}
```
### Custom output file path
You can specify where you want your types to be generated by adding a property to your Payload config:

View File

@@ -2,8 +2,6 @@
import fs from 'fs'
import { compile } from 'json-schema-to-typescript'
import type { SanitizedCollectionConfig, SanitizedGlobalConfig } from '../exports/types'
import payload from '..'
import loadConfig from '../config/load'
import { configToJSONSchema } from '../utilities/configToJSONSchema'
@@ -25,8 +23,8 @@ export async function generateTypes(): Promise<void> {
const jsonSchema = configToJSONSchema(payload.config, payload.db.defaultIDType)
const declare = `declare module "payload" {\n export interface GeneratedTypes extends Config {}\n}`
const declare = `declare module 'payload' {\n export interface GeneratedTypes extends Config {}\n}`
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*/',
@@ -34,7 +32,10 @@ export async function generateTypes(): Promise<void> {
singleQuote: true,
},
}).then((compiled) => {
fs.writeFileSync(outputFile, `${compiled}\n\n${declare}`)
if (config.typescript.declare !== false) {
compiled += `\n\n${declare}`
}
fs.writeFileSync(outputFile, compiled)
logger.info(`Types written to ${outputFile}`)
})
}

View File

@@ -178,6 +178,7 @@ export default joi.object({
}),
telemetry: joi.boolean(),
typescript: joi.object({
declare: joi.boolean(),
outputFile: joi.string(),
}),
upload: joi.object(),

View File

@@ -660,6 +660,8 @@ export type Config = {
telemetry?: boolean
/** Control how typescript interfaces are generated from your collections. */
typescript?: {
/** Disable declare block in generated types file */
declare?: false
/** Filename to write the generated types to */
outputFile?: string
}