chore(templates): add type detection on getGlobal utility#11617
Merged
Conversation
paulpopus
requested changes
Mar 11, 2025
paulpopus
left a comment
Contributor
There was a problem hiding this comment.
Thank you for working through this, definitely an improved change.
In the instances where these functions are then used, could you please remove the type assignment? Eg for the header
Changing
const headerData: Header = await getCachedGlobal('header', 1)()
To
const headerData = await getCachedGlobal('header', 1)()
And removing the imports please?
paulpopus
reviewed
Mar 12, 2025
Contributor
|
There's a werid CI error, can you pull the latest main and merge it into this branch please? @pierrecabriere |
Contributor
|
🚀 This is included in version v3.82.0 |
milamer
pushed a commit
to milamer/payload
that referenced
this pull request
Apr 20, 2026
…#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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 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>) to the getGlobal function
Updated getCachedGlobal to use the same generic typing pattern
These changes maintain the same runtime behavior while enhancing type safety