Automatic Collection/Global types (+ making type generation redundant) #1320
echocrow
started this conversation in
Feature Requests & Ideas
Replies: 2 comments 4 replies
-
If there is any interest in this, happy to help out with this! If so, to determine feasibility, would it make sense to start with a proof of concept for a subset of field types first? |
Beta Was this translation helpful? Give feedback.
0 replies
-
I don't think you can get around type generation here, since you also need to account for fields being added/modified dynamically at runtime, e.g. as a result of plugins. The interesting part would be to get Payload to automatically pick these types up where possible (maybe some type of mapping from slugs to generated types). |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, hooks on Collections and Globals are predominantly typed as
any
, as Payload cannot infer the shape of the doc.For example:
In order to infer or cast the type of a collection, a separate type generation process needs to be run via
payload generate:types
.However, I believe there might be a way to automatically infer the type of a given collection, based on the shape of its
field
values and other properties. The idea would be similar to what tRPC is doing, where the type is automatically inferred from its own definition - no code generation required.These automatically inferred types could then be fed into the hooks of the same definition, allowing for stricter typed
doc
variables.The same inferred types - perhaps even from a global Payload config level - could further be reused to benefit or drastically simplify #1319 for a more strictly typed Local API.
That said, I think this would require a different approach to typing globals and collections. Declaring…
…basically erases any further type info from TypeScript.
Instead, I reckon these would need to be declared
satisfies
operator.The latter is still in beta though, and the former might offer more granular control in the future.
What this could look like:
Beta Was this translation helpful? Give feedback.
All reactions