Closed tobbbe closed 3 years ago
Hi thank you for the kind words.
For this issue, I think you have two options:
import {Case} from '../your-types'
type CaseContentText = Extract<NonNullable<Case['content']>[number], {_type: 'textSection'}>
There's a lot of TypeScript going on there so let me break it down:
import {Case} from '../your-types'
type Content = Case['content'] // grab the property using index-access
type NonNullableContext = NonNullable<Content> // remove undefined from the type
type Item = NonNullableContext[number] // unwrap the type from the array using index-access again
type CaseContentText = Extract<Item, {_type: 'textSection'}> // https://stackoverflow.com/a/52943170/5776910
This answer https://github.com/ricokahler/sanity-codegen/discussions/196#discussioncomment-1359118 is relevant too
Wow, wow, WOW! Thank you so much! That solves my question and taught me some very interesting typescript and sanity stuff π Have a great day!
Hello! thanks for a great package!
Would it be possible to flatten all types? It would be great because right now I cant, for example, pass a nested property to a helper render function:
ex:
Right now this is what we get:
But this would help
Because then we can import the nested types (CaseTextSection and CaseContentGallery) and use them in
function TextSection/GallerySection
π