prismicio / gatsby-source-prismic-graphql

Gatsby source plugin for Prismic GraphQL
MIT License
17 stars 14 forks source link

fix(withpreview.tsx): narrow the type definition of withPreview.tsx #20

Closed matthewtgilbride closed 4 years ago

matthewtgilbride commented 4 years ago

The return type of withPreview.tsx was not explicitly declared before, leaving typescript to infer it. The inferred type was not compatible with what gatsby's StaticQuery component expected, causing users to need to cast it themselves, which is less than ideal.

matthewtgilbride commented 4 years ago

@MarcMcIntosh let me know your thoughts...

MarcMcIntosh commented 4 years ago

Hi @matthewtgilbride so I'm taking a look at gatsby/index.d.ts as well. soecifically lines 136 to 142.

type RenderCallback<T = any> = (data: T) => React.ReactNode

export interface StaticQueryProps<T = any> {
  query: any
  render?: RenderCallback<T>
  children?: RenderCallback<T>
}

and I'm wondering if export type StaticQueryRender<T> = Required<StaticQueryProps<T>>['render']; is similar to type RenderCallback<T = any> = (data: T) => React.ReactNode

MarcMcIntosh commented 4 years ago

You can also add your name to the contributors in the package.json btw :)

matthewtgilbride commented 4 years ago

Hi @matthewtgilbride so I'm taking a look at gatsby/index.d.ts as well. soecifically lines 136 to 142.

type RenderCallback<T = any> = (data: T) => React.ReactNode

export interface StaticQueryProps<T = any> {
  query: any
  render?: RenderCallback<T>
  children?: RenderCallback<T>
}

and I'm wondering if export type StaticQueryRender<T> = Required<StaticQueryProps<T>>['render']; is similar to type RenderCallback<T = any> = (data: T) => React.ReactNode

Yep, so the problem is that type RenderCallback... isn't exported, so the only way to access it is via the lookup type StaticQueryProps['render']`.

Then, I'm wrapping that in Required to make it non-optional (I'm not sure why it's optional in the first place...if there is a use case for using the StaticQuery component without actually rendering anything, please educate me).

matthewtgilbride commented 4 years ago

You can also add your name to the contributors in the package.json btw :)

OK : - >

matthewtgilbride commented 4 years ago

@MarcMcIntosh I've adjusted this change to have the smallest surface area possible. Basically, making the render argument as well as the return type be (data: Data) => JSX.Element satisfies the compiler in the case withPreview is used in conjunction with the StaticQuery HOC.

Using it with the useStaticQuery hook compiles both before and after this change.

Once this is merged I will remove the type assertions from the typescript example (PR #24).

MarcMcIntosh commented 4 years ago

Neet, thanks :)