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

[Bug]: In addOptions. the type of this.parent is wrong. #5768

Open
1 task done
mgreystone opened this issue Oct 25, 2024 · 6 comments
Open
1 task done

[Bug]: In addOptions. the type of this.parent is wrong. #5768

mgreystone opened this issue Oct 25, 2024 · 6 comments
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug

Comments

@mgreystone
Copy link
Contributor

mgreystone commented Oct 25, 2024

Affected Packages

core

Version(s)

2.9.1

Bug Description

When extending a node & implementing addOptions, the type of this.parent is incorrect.

For a Node<T>, this.parent is typed as () => T, denoting that it is safe to call directly like this.parent(). However, this.parent might be undefined, as is documented, so we must call this.parent?.().

Because of the inaccurate type, it is easy to forget to add the ?. optional chaining. Furthermore, linting rules such as no-unnecessary-condition can automatically strip the ?. out, which can cause type errors be thrown at runtime.

import { Heading, type Level } from '@tiptap/extension-heading'

const MyHeading = Heading.extend({
  addOptions() {
    const levels: Level[] = [1,2,3]
    return {
      ...this.parent(), // this throws even though typescript says it is safe
      levels,
    }
  }
})

Browser Used

Chrome

Code Example URL

No response

Expected Behavior

In the addOptions method, this.parent should have a type of (() => T) | undefined to match the implementation & documentation.

Additional Context (Optional)

No response

Dependency Updates

  • Yes, I've updated all my dependencies.
@mgreystone mgreystone added Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug labels Oct 25, 2024
@nperez0111
Copy link
Contributor

Yep, you are right here. Pushing a patch for it

@nperez0111
Copy link
Contributor

Resolved with 2.10.0

@nperez0111
Copy link
Contributor

Reopening because I'm going to revert this change. It is more trouble than it is worth. If someone can find a way to update the typings in a way that don't break people. Feel free to contribute

nperez0111 added a commit that referenced this issue Nov 22, 2024
* revert: "fix(core): update the typings to be that options and storage are partials on an extended config #5852 (#5854)"

This reverts commit 87d63d8.

* revert: "fix(core): update the typing of `addOptions`, `addStorage` to have an optional parent #5768 (#5770)"

This reverts commit d2f366d.
@Mathias-S
Copy link
Contributor

Mathias-S commented Nov 22, 2024

Some context: The attempts to resolve this issue caused the following new type errors in code that was previously working:

import Link from "@tiptap/extension-link";

const MyLink = Link.extend({
  addOptions() {
//^^^^^^^^^^ Type error
    return {
      ...this.parent?.(),
    };
  },
});

See #5852 for the issue about the error above.

import Paragraph from "@tiptap/extension-paragraph";
import { mergeAttributes } from "@tiptap/react";

const MyStrangeParagraph = Paragraph.extend({
  renderHTML({ HTMLAttributes }) {
    return [
      `h1`,
      mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
      //              ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 'undefined' is not assignable to 'Record<string, any>'
      0,
    ];
  },
});

See #5859 for more details about the error above.

So any attempts to fix this issue should also ensure that the aforementioned cases won't have any type errors.

gethari pushed a commit to gethari/tiptap that referenced this issue Nov 23, 2024
…osis#5860)

* revert: "fix(core): update the typings to be that options and storage are partials on an extended config ueberdosis#5852 (ueberdosis#5854)"

This reverts commit 87d63d8.

* revert: "fix(core): update the typing of `addOptions`, `addStorage` to have an optional parent ueberdosis#5768 (ueberdosis#5770)"

This reverts commit d2f366d.
@mgreystone
Copy link
Contributor Author

That's a bummer, but i understand that it would require a breaking change to the types. Do we think that the change would be acceptable in version 3? We have to remember to add // eslint-disable-next-line no-unnecessary-condition before all of our uses of this.parent?.() or else our eslint config auto-strips out all of the nullish chaining, which more often than not causes runtime errors.

@nperez0111
Copy link
Contributor

I would be willing to get it in version 2 if there were an easy way to handle it.

I tired doing this for like an hour and basically what I had come up with was that the types of the first .create needs to be different than a .extend. This isn't the easiest thing to support especially since it is already generic over two parameters so it gets confusing very fast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug
Projects
None yet
Development

No branches or pull requests

3 participants