react-email-editor icon indicating copy to clipboard operation
react-email-editor copied to clipboard

Property 'editor' does not exist on type 'never'.

Open MaxMcNally opened this issue 2 years ago • 1 comments

When using Typescript, the react example gives the error - Property 'editor' does not exist on type 'never'. where ever emailEditorRef.current.editor is referenced. Any ideas of how to fix this? Screen Shot 2022-07-24 at 10 33 25 AM

MaxMcNally avatar Jul 24 '22 17:07 MaxMcNally

I have simply TS ignored the error because it is functional code, just has no TS support.

In theory, you could write your own explicit typing to fix the error but the complete lack of documentation makes it nearly impossible...

driaug avatar Jul 30 '22 19:07 driaug

I tried adding any as reference type and it worked. So with typescript support on the starter code in readme would look as below

import React, { useRef } from "react";
import { render } from "react-dom";

import EmailEditor from "react-email-editor";

export default () => {
  const emailEditorRef = useRef<any>(null);

  const exportHtml = () => {
    emailEditorRef.current!.editor.exportHtml((data: any) => {
      const { design, html } = data;
      console.log("exportHtml", html);
    });
  };

  const onLoad = () => {
    // editor instance is created
    // you can load your template here;
    // const templateJson = {};
    // emailEditorRef.current.editor.loadDesign(templateJson);
  };

  const onReady = () => {
    // editor is ready
    console.log("onReady");
  };

  return (
    <div>
      <div>
        <button onClick={exportHtml}>Export HTML</button>
      </div>

      <EmailEditor ref={emailEditorRef} onLoad={onLoad} onReady={onReady} />
    </div>
  );
};

akleshsakunia avatar Sep 25 '22 20:09 akleshsakunia

Try defining the ref like this:

const emailEditorRef = useRef<InstanceType<typeof EmailEditor> | null>(null);

Then using the ref without accessing the editor property will give you intellisense: type-fix

sean-beard avatar Oct 06 '22 23:10 sean-beard

The above given solution doesn't seem to work for me. It seems that the package has been updated last month (10th feb) with types.

I now got this working as follows:

const emailEditorRef = useRef<EditorRef | null>(null);
const exportHtml = () => {
    const reference = emailEditorRef.current;
    if (reference && reference.editor !== null) {
        reference.editor.exportHtml((data) => {
            const { design, html } = data;
            console.log("exportHtml", html);
        });
    }
};

This gives the appropriate types for the data object.

Another weird issue I found in the types is that the onLoad function is marked as depricated. But seeing how it's still in their getting started example, I'm not sure what is going on here.

timsun28 avatar Mar 07 '23 19:03 timsun28

Please update to the latest version. We have types now.

@timsun28 I have updated the getting started example, you should use onReady instead of onLoad

lucasbesen avatar Mar 10 '23 05:03 lucasbesen