react-pdf icon indicating copy to clipboard operation
react-pdf copied to clipboard

Document and Page Typescript typings are missing a children property

Open pganster opened this issue 4 years ago • 7 comments

Describe the bug As of lately, neither Page nor Document component can be used with children. I've copied the code from the quick start guide on https://react-pdf.org/ into a freshly created React app and receive the following Typescript issues:

ERROR in src/index.tsx:23:4
TS2769: No overload matches this call.
  Overload 1 of 2, '(props: DocumentProps | Readonly<DocumentProps>): Document', gave the following error.
    Type '{ children: Element; }' has no properties in common with type 'IntrinsicAttributes & IntrinsicClassAttributes<Document> & Readonly<DocumentProps>'.
  Overload 2 of 2, '(props: DocumentProps, context: any): Document', gave the following error.
    Type '{ children: Element; }' has no properties in common with type 'IntrinsicAttributes & IntrinsicClassAttributes<Document> & Readonly<DocumentProps>'.
    21 | // Create Document Component
    22 | const MyDocument = () => (
  > 23 |   <Document>
       |    ^^^^^^^^
    24 |     <Page size="A4" style={styles.page}>
    25 |       <View style={styles.section}>
    26 |         <Text>Section #1</Text>

ERROR in src/index.tsx:24:6
TS2769: No overload matches this call.
  Overload 1 of 2, '(props: PageProps | Readonly<PageProps>): Page', gave the following error.
    Type '{ children: Element[]; size: "A4"; style: { flexDirection: "row"; backgroundColor: string; }; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Page> & Readonly<PageProps>'.
      Property 'children' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Page> & Readonly<PageProps>'.
  Overload 2 of 2, '(props: PageProps, context: any): Page', gave the following error.
    Type '{ children: Element[]; size: "A4"; style: { flexDirection: "row"; backgroundColor: string; }; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Page> & Readonly<PageProps>'.
      Property 'children' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Page> & Readonly<PageProps>'.
    22 | const MyDocument = () => (
    23 |   <Document>
  > 24 |     <Page size="A4" style={styles.page}>
       |      ^^^^
    25 |       <View style={styles.section}>
    26 |         <Text>Section #1</Text>
    27 |       </View>

As far as I can see is the issue that both Page and Document need a children property definition in PageProps and DocumentProps respectively. The View component already has this. It is probably related to this issue: https://github.com/diegomura/react-pdf/issues/1800.

To Reproduce Steps to reproduce the behavior including code snippet (if applies):

  1. Create a new Create React App npx create-react-app test-app --template typescript
  2. Add the required dependencies yarn add @react-pdf/renderer @craco/craco process assert browserify-zlib buffer stream-browserify
  3. Setup Craco by adding a craco.config.js file to the root:
const webpack = require("webpack");

// In react-scripts 5, webpack was upgraded to 5 which removed the support for node builtins.
// These polyfills are needed for react-pdf:
// https://github.com/diegomura/react-pdf/issues/1645#issuecomment-999072302
// Therefore we need craco to configure webpack (without ejecting react-scripts) to add back
// the removed node builtins.
// https://github.com/facebook/create-react-app/pull/11764
module.exports = {
  webpack: {
    configure: {
      resolve: {
        fallback: {
          process: require.resolve("process/browser"),
          zlib: require.resolve("browserify-zlib"),
          stream: require.resolve("stream-browserify"),
          util: require.resolve("util"),
          buffer: require.resolve("buffer"),
          asset: require.resolve("assert"),
        },
      },
      plugins: [
        new webpack.ProvidePlugin({
          Buffer: ["buffer", "Buffer"],
          process: "process/browser",
        }),
      ],
    },
  },
};
  1. Add the code snippet to the index.tsx:
import { Page, Text, View, Document, StyleSheet } from '@react-pdf/renderer';

// Create styles
const styles = StyleSheet.create({
  page: {
    flexDirection: 'row',
    backgroundColor: '#E4E4E4'
  },
  section: {
    margin: 10,
    padding: 10,
    flexGrow: 1
  }
});

// Create Document Component
const MyDocument = () => (
  <Document>
    <Page size="A4" style={styles.page}>
      <View style={styles.section}>
        <Text>Section #1</Text>
      </View>
      <View style={styles.section}>
        <Text>Section #2</Text>
      </View>
    </Page>
  </Document>
);
  1. Start the app with yarn run craco start
  2. Receive the error

Expected behavior No typescript errors.

pganster avatar May 06 '22 11:05 pganster

i have the same error on an express app. I downgradet the react version to 17.0.2. Then the error ist gone as Workaround.

papack avatar May 09 '22 14:05 papack

I am facing the same issue with react 18.0.0 and react-pdf 2.1.1. Adding the children prop manually fixed it.

ILucasI avatar May 14 '22 11:05 ILucasI

I am facing the same issue with react 18.0.0 and react-pdf 2.1.1. Adding the children prop manually fixed it.

Can you share a snippet indicating how to do this? Edit: Here is how https://github.com/diegomura/react-pdf/issues/1779#issuecomment-1094858786

akshay-nm avatar May 17 '22 04:05 akshay-nm

In the index.d.ts file (inside node_modules/@react-pdf/renderer) you have to add children?: React.ReactNode; to the DocumentProps and PageProps

image

image

I am facing the same issue with react 18.0.0 and react-pdf 2.1.1. Adding the children prop manually fixed it.

Can you share a snippet indicating how to do this? Edit: Here is how #1779 (comment)

ILucasI avatar May 17 '22 06:05 ILucasI

I proposed a solution: https://github.com/diegomura/react-pdf/pull/1825.

allondeveen avatar May 24 '22 08:05 allondeveen

after the PR Document and Page works for me 🎉. but I now have the same errors with and does anyone know where to add the types there?

papack avatar May 31 '22 13:05 papack

Thanks @allondeveen the problem seems to be solved.

pganster avatar May 31 '22 15:05 pganster