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

Using react-to-pdf with typescript

Open olabamipetaiwo opened this issue 3 years ago • 2 comments

This package doesn't have a types file to use with typ[escript

olabamipetaiwo avatar Jan 05 '22 00:01 olabamipetaiwo

Any news on this?

loki344 avatar Jul 26 '22 06:07 loki344

Has anyone managed to solve this problem?

israelitalo avatar Sep 06 '22 13:09 israelitalo

I solve this issue. Firstly, Create a folder typings then create file index.d.ts inside the folder. then declare module declare module 'react-to-pdf'. Then add "typeRoots": [ "./typings", "./node_modules/@types" ] inside tsconfig.json.

Folder Structure:

  • typings
    • index.d.ts `declare module 'react-to-pdf'

ts-config rror-ts7016-could-not-find-a-declaration-file-for-module-xyz-has-an-any-type-ecab588800a8)

[Reference](https://pjausovec.medium.com/how-to-fix-e

sajeebmahamed avatar Nov 24 '22 08:11 sajeebmahamed

I solve this issue. Firstly, Create a folder typings then create file index.d.ts inside the folder. then declare module declare module 'react-to-pdf'. Then add "typeRoots": [ "./typings", "./node_modules/@types" ] inside tsconfig.json.

Folder Structure:

  • typings

    • index.d.ts `declare module 'react-to-pdf'

ts-config rror-ts7016-could-not-find-a-declaration-file-for-module-xyz-has-an-any-type-ecab588800a8)

[Reference](https://pjausovec.medium.com/how-to-fix-e

This doesn't work for me

Auspicious14 avatar Jan 26 '23 12:01 Auspicious14

It works now but there's still error in the code as {toPdf} still has an implicit type of any with red underline

Auspicious14 avatar Jan 26 '23 12:01 Auspicious14

Try declaring a module like this in a file that ends with .d.ts:

declare module 'react-to-pdf' {
    import { RefObject } from 'react';

    function ReactToPdf(
        targetRef?: RefObject,
        filename?: string,
        x?: number,
        y?: number,
        options?: object,
        onComplete?: function,
        scale: number
    ): ReactNode<{ toPdf: () => void; targetRef: MutableRefObject }>;

    export = ReactToPdf;
}

You still have to explicitly add a typing to toPdf like this: {({ toPdf }: never) => ()}

meessour avatar Mar 06 '23 11:03 meessour

v1 has been just released and it's built with TS, please see README for more details/examples.

ivmarcos avatar Sep 07 '23 17:09 ivmarcos