I am using graphql codegen together with urql to ensure back-to-back ts safety. But, after generating types and queries, I am having an issue typing the component props when passing data down to the component. Here is an example:
This is the query type generated:
export type AnnotationsQuery = { __typename?: 'Query', threads?: Array<{ __typename?: 'Thread', annotation: { __typename?: 'Annotation', id: string, x: number, y: number, width: number, height: number, key: number, color: string, stroke: number, style: string } }> | null };
Normally, when mapping over threads
and passing thread
as a prop to the component it could be typed as AnnotationsQuery["threads"][number]
but in this case this will not work as the threads?: Array<>
is optional, making AnnotationsQuery["threads"][number]
not a proper type or any
.
Is there a better/correct way to type a single thread
even if the threads
is optional?