prismicio / prismic-react

React components and hooks to fetch and present Prismic content
https://prismic.io/docs/technologies/homepage-reactjs
Apache License 2.0
154 stars 40 forks source link

PrismicProvider + Typescript + linkResolver #137

Closed kb1995 closed 2 years ago

kb1995 commented 2 years ago

I'm using the default guide of setting up a project. My Link resolver looks like this

export function linkResolver(doc) {
  switch (doc.type) {
    case "homepage":
      return "/";
    case "page":
      return `/${doc.uid}`;
    default:
      return null;
  }
}

Notice the null return which I think is necessary if you want to use it in combination with Route Resolver.

my _app.tsx (next.js app) looks like this:

import '../styles/globals.css'
import type { AppProps } from 'next/app'
import Link from "next/link";
import { PrismicProvider } from "@prismicio/react";
import { PrismicPreview } from "@prismicio/next";
import { linkResolver, repositoryName } from "../prismicio";

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <PrismicProvider
      linkResolver={linkResolver}
      internalLinkComponent={({ href, children, ...props }) => (
        <Link href={href}>
          <a {...props}>{children}</a>
        </Link>
      )}
    >
      <PrismicPreview repositoryName={repositoryName}>
        <Component {...pageProps} />
      </PrismicPreview>
    </PrismicProvider>
  );
}

export default MyApp

I get an error on the PRismicProvider's linkResolver prop

Type '(doc: any) => string | null' is not assignable to type 'LinkResolverFunction<string>'.
  Type 'string | null' is not assignable to type 'string'.
    Type 'null' is not assignable to type 'string'.ts(2322)
[index.d.ts(129, 5): ]()The expected type comes from property 'linkResolver' which is declared here on type 'IntrinsicAttributes & PrismicContextValue'
angeloashmore commented 2 years ago

Hey @kb1995, sorry, I just realized I didn’t reply to you before closing the issue.

This should be fixed in v2.2.0.

You can see details about the fix here: #142

Thanks for reporting this! If the fix doesn’t work for you, please let me know and I’ll take another look. 🙂