Skip to content

Commit b278a17

Browse files
chore(templates): add type detection on getGlobal utility (#11617)
### What? Added proper type detection to the global data fetching utilities in the form builder example. Enhanced the getGlobal and getCachedGlobal functions to leverage TypeScript generics for improved type safety. #### Why? This change improves developer experience by providing proper TypeScript type inference when working with global data. With these changes: The returned data from getGlobal is now properly typed as DataFromGlobalSlug<T> based on the slug parameter TypeScript can now correctly infer the return type of getCachedGlobal based on the global slug This prevents type errors and provides better IDE autocomplete/IntelliSense when accessing properties on retrieved global data ### How? Added import for the DataFromGlobalSlug type from Payload Made getGlobal a generic function that accepts a type parameter T extends Global Added proper return type annotation (Promise<DataFromGlobalSlug<T>>) to the getGlobal function Updated getCachedGlobal to use the same generic typing pattern These changes maintain the same runtime behavior while enhancing type safety
1 parent 05ddec1 commit b278a17

11 files changed

Lines changed: 23 additions & 36 deletions

File tree

examples/form-builder/src/components/Header/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { ModalToggler } from '@faceless-ui/modal'
22
import Link from 'next/link'
33
import React from 'react'
44

5-
import type { MainMenu } from '../../payload-types'
6-
75
import { getCachedGlobal } from '../../utilities/getGlobals'
86
import { Gutter } from '../Gutter'
97
import { MenuIcon } from '../icons/Menu'
@@ -35,7 +33,7 @@ export const HeaderBar: React.FC<HeaderBarProps> = ({ children }) => {
3533
}
3634

3735
export async function Header() {
38-
const header: MainMenu = await getCachedGlobal('main-menu', 1)()
36+
const header = await getCachedGlobal('main-menu', 1)()
3937

4038
const navItems = header?.navItems || []
4139

examples/form-builder/src/utilities/getGlobals.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { Config } from 'src/payload-types'
22

33
import { unstable_cache } from 'next/cache'
4-
import { getPayload } from 'payload'
4+
import { type DataFromGlobalSlug, getPayload } from 'payload'
55

66
import configPromise from '../payload.config'
77

88
type Global = keyof Config['globals']
99

10-
async function getGlobal(slug: Global, depth = 0) {
10+
async function getGlobal<T extends Global>(slug: T, depth = 0): Promise<DataFromGlobalSlug<T>> {
1111
const payload = await getPayload({ config: configPromise })
1212

1313
const global = await payload.findGlobal({
@@ -21,7 +21,7 @@ async function getGlobal(slug: Global, depth = 0) {
2121
/**
2222
* Returns a unstable_cache function mapped with the cache tag for the slug
2323
*/
24-
export const getCachedGlobal = (slug: Global, depth = 0) =>
25-
unstable_cache(async () => getGlobal(slug, depth), [slug], {
24+
export const getCachedGlobal = <T extends Global>(slug: T, depth = 0) =>
25+
unstable_cache(async () => getGlobal<T>(slug, depth), [slug], {
2626
tags: [`global_${slug}`],
2727
})

examples/localization/src/globals/Footer/Component.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ import { getCachedGlobal } from '@/utilities/getGlobals'
22
import Link from 'next/link'
33
import React from 'react'
44

5-
import type { Footer } from '@/payload-types'
6-
75
import { ThemeSelector } from '@/providers/Theme/ThemeSelector'
86
import { CMSLink } from '@/components/Link'
97
import { TypedLocale } from 'payload'
108

119
export async function Footer({ locale }: { locale: TypedLocale }) {
12-
const footer: Footer = await getCachedGlobal('footer', 1, locale)()
10+
const footer = await getCachedGlobal('footer', 1, locale)()
1311

1412
const navItems = footer?.navItems || []
1513

examples/localization/src/globals/Header/Component.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { HeaderClient } from './Component.client'
22
import { getCachedGlobal } from '@/utilities/getGlobals'
33
import React from 'react'
44

5-
import type { Header } from '@/payload-types'
65
import { TypedLocale } from 'payload'
76

87
export async function Header({ locale }: { locale: TypedLocale }) {
9-
const header: Header = await getCachedGlobal('header', 1, locale)()
8+
const header = await getCachedGlobal('header', 1, locale)()
109

1110
return <HeaderClient header={header} />
1211
}

examples/localization/src/utilities/getGlobals.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { Config } from 'src/payload-types'
22

33
import configPromise from '@payload-config'
4-
import { getPayload } from 'payload'
4+
import { type DataFromGlobalSlug, getPayload } from 'payload'
55
import { unstable_cache } from 'next/cache'
66
import { TypedLocale } from 'payload'
77

88
type Global = keyof Config['globals']
99

10-
async function getGlobal(slug: Global, depth = 0, locale: TypedLocale) {
10+
async function getGlobal<T extends Global>(slug: T, depth = 0, locale: TypedLocale): Promise<DataFromGlobalSlug<T>> {
1111
const payload = await getPayload({ config: configPromise })
1212

1313
const global = await payload.findGlobal({
@@ -22,7 +22,7 @@ async function getGlobal(slug: Global, depth = 0, locale: TypedLocale) {
2222
/**
2323
* Returns a unstable_cache function mapped with the cache tag for the slug and locale
2424
*/
25-
export const getCachedGlobal = (slug: Global, depth = 0, locale: TypedLocale) =>
26-
unstable_cache(async () => getGlobal(slug, depth, locale), [slug, locale], {
25+
export const getCachedGlobal = <T extends Global>(slug: T, depth = 0, locale: TypedLocale) =>
26+
unstable_cache(async () => getGlobal<T>(slug, depth, locale), [slug, locale], {
2727
tags: [`global_${slug}`],
2828
})

templates/website/src/Footer/Component.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ import { getCachedGlobal } from '@/utilities/getGlobals'
22
import Link from 'next/link'
33
import React from 'react'
44

5-
import type { Footer } from '@/payload-types'
6-
75
import { ThemeSelector } from '@/providers/Theme/ThemeSelector'
86
import { CMSLink } from '@/components/Link'
97
import { Logo } from '@/components/Logo/Logo'
108

119
export async function Footer() {
12-
const footerData: Footer = await getCachedGlobal('footer', 1)()
10+
const footerData = await getCachedGlobal('footer', 1)()
1311

1412
const navItems = footerData?.navItems || []
1513

templates/website/src/Header/Component.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { HeaderClient } from './Component.client'
22
import { getCachedGlobal } from '@/utilities/getGlobals'
33
import React from 'react'
44

5-
import type { Header } from '@/payload-types'
6-
75
export async function Header() {
8-
const headerData: Header = await getCachedGlobal('header', 1)()
6+
const headerData = await getCachedGlobal('header', 1)()
97

108
return <HeaderClient data={headerData} />
119
}

templates/website/src/utilities/getGlobals.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { Config } from 'src/payload-types'
22

33
import configPromise from '@payload-config'
4-
import { getPayload } from 'payload'
4+
import { type DataFromGlobalSlug, getPayload } from 'payload'
55
import { unstable_cache } from 'next/cache'
66

77
type Global = keyof Config['globals']
88

9-
async function getGlobal(slug: Global, depth = 0) {
9+
async function getGlobal<T extends Global>(slug: T, depth = 0): Promise<DataFromGlobalSlug<T>> {
1010
const payload = await getPayload({ config: configPromise })
1111

1212
const global = await payload.findGlobal({
@@ -20,7 +20,7 @@ async function getGlobal(slug: Global, depth = 0) {
2020
/**
2121
* Returns a unstable_cache function mapped with the cache tag for the slug
2222
*/
23-
export const getCachedGlobal = (slug: Global, depth = 0) =>
24-
unstable_cache(async () => getGlobal(slug, depth), [slug], {
23+
export const getCachedGlobal = <T extends Global>(slug: T, depth = 0) =>
24+
unstable_cache(async () => getGlobal<T>(slug, depth), [slug], {
2525
tags: [`global_${slug}`],
2626
})

templates/with-vercel-website/src/Footer/Component.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ import { getCachedGlobal } from '@/utilities/getGlobals'
22
import Link from 'next/link'
33
import React from 'react'
44

5-
import type { Footer } from '@/payload-types'
6-
75
import { ThemeSelector } from '@/providers/Theme/ThemeSelector'
86
import { CMSLink } from '@/components/Link'
97
import { Logo } from '@/components/Logo/Logo'
108

119
export async function Footer() {
12-
const footerData: Footer = await getCachedGlobal('footer', 1)()
10+
const footerData = await getCachedGlobal('footer', 1)()
1311

1412
const navItems = footerData?.navItems || []
1513

templates/with-vercel-website/src/Header/Component.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { HeaderClient } from './Component.client'
22
import { getCachedGlobal } from '@/utilities/getGlobals'
33
import React from 'react'
44

5-
import type { Header } from '@/payload-types'
6-
75
export async function Header() {
8-
const headerData: Header = await getCachedGlobal('header', 1)()
6+
const headerData = await getCachedGlobal('header', 1)()
97

108
return <HeaderClient data={headerData} />
119
}

0 commit comments

Comments
 (0)