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

Error in signature of withReact #4144

Closed
ChildishForces opened this issue Mar 27, 2021 · 7 comments · Fixed by #5091
Closed

Error in signature of withReact #4144

ChildishForces opened this issue Mar 27, 2021 · 7 comments · Fixed by #5091

Comments

@ChildishForces
Copy link

ChildishForces commented Mar 27, 2021

Description
Attempting to start a new Typescript project on @next is causing interface issues from the 1st line.

const editor = React.useMemo(() => withReact(createEditor()), []);

Immediately complains because createEditor() returns a BaseEditor and the definition for withReact requires a generic type T that extends from ReactEditor and then returns a union of T & ReactEditor which doesn't seem right.

Recording
(Definition: slate-react/dist/plugin/with-react.d.ts)

Steps
To reproduce the behavior:

  1. On a fresh TS app write const editor = React.useMemo(() => withReact(createEditor()), []);
  2. See error

Expectation
I expect this is an error, and that withReact wants a generic extending BaseEditor

Environment

  • Slate Version: [e.g. 0.60.15]
  • Operating System: [N/A]
  • Browser: [N/A]
  • TypeScript Version: 4.1.5
@mitchheddles
Copy link

mitchheddles commented Apr 3, 2021

@ChildishForces I had the same issue, but was able to fix it by defining a custom Editor.

Check out the typescript docs https://github.com/ianstormtaylor/slate/blob/main/docs/concepts/11-typescript.md#defining-editor-element-and-text-types

@lensbart
Copy link

lensbart commented Apr 8, 2021

@santialbo
Copy link

This is weird, the types are correct in the source

export const withReact = <T extends Editor>(editor: T) => {

However on the generated type definitions they are wrong

export declare const withReact: <T extends ReactEditor>(editor: T) => T & ReactEditor;

should be

export declare const withReact: <T extends Editor>(editor: T) => T & ReactEditor;

@abdessamadely
Copy link

I'm facing the same issue, seems like incompatible types, with some missing properties
as a work around I'm casting it to ReactEditor like so:

const editor = useMemo(() => withReact(createEditor() as ReactEditor), []);

as it doesn't seem to cause any compilation issues

@johnsorianodev
Copy link

Same here, but I was able to resolve like the implementation of @abdessamadelhamdany

@abdessamadely
Copy link

Hi @iamdddev I think you just need to add custom types like in this example: https://github.com/ianstormtaylor/slate/blob/main/site/examples/custom-types.d.ts

Slate has a little different view of how typescript types implemented

e1himself added a commit to e1himself/slate that referenced this issue Aug 19, 2022
@e1himself
Copy link
Contributor

There is indeed a problem with withReact() signature.

I've filed a PR to fix it: #5091

dylans pushed a commit that referenced this issue Aug 22, 2022
* Fix signature of `withReact` function

Fixes #4144

* Fix code to pass type checks

* Add a changeset entry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants