Hey ,
I was reading through the blog post How To Generate TypeScript Types From GraphQL and encountered the following problem when I have a query retrieving a Structured text field.
The âvalueâ parameter has the type âunknownâ in the generated type which causes an TypeScript error on the data
prop when using the <StructuredText />
component:
Type âunknownâ is not assignable to type âDocument | Node | StructuredText<Record, Record> | null | undefinedâ.ts(2322)
Here is my query:
query FrontPage($id: ItemId) {
landingFrontPage(filter: { id: { eq: $id } }) {
name
structuredContent {
value
}
}
}
Here the generated type:
export type FrontPageQuery = {
__typename?: "Query"
landingFrontPage?: {
__typename?: "LandingFrontPageRecord"
name: string
structuredContent?: {
__typename?: "LandingFrontPageModelStructuredContentField"
value: unknown
} | null
} | null
}
In my codegen.yml
I used the custom scalar types found here: Custom Scalar Types â DatoCMS
Some observations
When I change the JsonField
from JsonField: unkown
to JsonField: "StructuredText<Record, Record>"
, in my codegen.yml
, in the generated FrontPageQuery
type the value
will be of type value: StructuredText<Record, Record>
which makes TypeScript happy again. Is this the right approach?
Best,
Martin