Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting TypeScript errors when using block component #12

Closed
petervukovic opened this issue Apr 13, 2022 · 2 comments
Closed

Getting TypeScript errors when using block component #12

petervukovic opened this issue Apr 13, 2022 · 2 comments

Comments

@petervukovic
Copy link

petervukovic commented Apr 13, 2022

Given this basic setup from the README:

const components = {
  block: {
    h1: ({children}) => <h1 className="text-2xl">{children}</h1>,
  }
};

<PortableText value={body} components={components} />

The TypeScript Engine will report this problem on the <PortableText> components property:

Type '{ block: { h1: ({ children }: { children: any; }) => Element; }; }' is not assignable to type 'Partial<PortableTextReactComponents>'.
  Types of property 'block' are incompatible.
    Type '{ h1: ({ children }: { children: any; }) => Element; }' is not assignable to type 'PortableTextBlockComponent | Record<string, PortableTextBlockComponent>'.
      Type '{ h1: ({ children }: { children: any; }) => Element; }' is not assignable to type 'Record<string, PortableTextBlockComponent>'.
        Property 'h1' is incompatible with index signature.
          Type '({ children }: { children: any; }) => Element' is not assignable to type 'PortableTextBlockComponent'.
            Type '({ children }: { children: any; }) => Element' is not assignable to type 'FunctionComponent<PortableTextComponentProps<PortableTextBlock<PortableTextMarkDefinition, ArbitraryTypedObject | PortableTextSpan, string, string>>>'.
              Types of parameters '__0' and 'props' are incompatible.
                Type 'PropsWithChildren<PortableTextComponentProps<PortableTextBlock<PortableTextMarkDefinition, ArbitraryTypedObject | PortableTextSpan, string, string>>>' is not assignable to type '{ children: any; }'.
                  Property 'children' is optional in type 'PortableTextComponentProps<PortableTextBlock<PortableTextMarkDefinition, ArbitraryTypedObject | PortableTextSpan, string, string>> & { ...; }' but required in type '{ children: any; }'.ts(2322)
[types.ts(32, 3): ]()The expected type comes from property 'components' which is declared here on type 'IntrinsicAttributes & PortableTextProps<any>'

The front-end renders fine, but I'm trying to deploy this using NextJS / Vercel and the build is failing.

Note that the problem isn't occurring when using marks, or types.

@petervukovic petervukovic changed the title Getting TypeScript Errors When using block component Getting TypeScript errors When using block component Apr 13, 2022
@petervukovic petervukovic changed the title Getting TypeScript errors When using block component Getting TypeScript errors when using block component Apr 13, 2022
@petervukovic
Copy link
Author

Just an update for anyone who stumbles upon this issue - you can resolve it by specifying the type for the components variable:


import { PortableText, PortableTextReactComponents } from "@portabletext/react";

const components:Partial<PortableTextReactComponents> = {
  block: {
    h1: ({children}) => <h1 className="text-2xl">{children}</h1>,
  }
};

@swalker326
Copy link

Can you close the issue, since it wasn't a repo issue and you found the solution?

@rexxars rexxars closed this as completed Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants