tiptap: [Bug]: TypeError/regression introduced in 2.10.1: All options can be `undefined`

Affected Packages

react, core, all extensions

Version(s)

2.10.1

Bug Description

Code that was previously working fine, now gives a TypeError when trying to access properties on this.options. These two examples were working fine in Tiptap 2.9, but now give errors:

Example 1

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,
    ];
  },
});

Example 2

import Link, { type LinkOptions } from "@tiptap/extension-link";

interface MyLinkOptions extends LinkOptions {
  myFunc: () => void;
}

const MyLink = Link.extend<MyLinkOptions>({
  addCommands() {
    return {
      ...this.parent?.(),
      myNewCommand: () => () => {
        this.options.myFunc();
//      ^^^^^^^^^^^^^^^^^^^ Object possibly 'undefined'
      },
    };
  },
});

MyLink.configure({ myFunc: () => undefined });

Browser Used

Chrome

Code Example URL

No response

Expected Behavior

For example 1, I would expect this to work. I’m not sure how this can be done without using a non-null assertion this.options.HTMLAttributes! or without writing extra logic that wasn’t previously necessary.

For example 2, I recognise that MyLink can be configured without passing in myFunc, so maybe the old code was wrong and there should always have been a nullish check such as this.options?.myFunc(). But shouldn’t it be possible to provide mandatory options?

Additional Context (Optional)

Introduced with release 2.10.1 with #5854.

Also, I understand that the options types may be more correct now, but these changes are breaking type changes and according to Renovatebot, which we are using in our project, the passing percentage for this update is quite low, indicating there have been breaking changes. I don’t know if this specific type change is the breaking change for everyone, but it is for us. See the below screenshot that shows Renovatebot stats for some of the packages.

image

Dependency Updates

  • Yes, I’ve updated all my dependencies.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 17 (17 by maintainers)

Commits related to this issue

Most upvoted comments

No, the implementation is the one that i wrong here, how it works in the docs is the way that it is meant to work. but, this needs some work to get it to work again like my diff was able to.

Would need this change for Mark, Extension and Node classes.

Would take a PR to fix this

Well look at that, addOptions is indeed incorrect here. What I said would be true for other things though like addAttributes, options are just treated differently and initialized immediately.

I was able to get options work with:

diff --git a/demos/src/Marks/Link/React/index.jsx b/demos/src/Marks/Link/React/index.jsx
index 4af6bcff6..43c45d6b3 100644
--- a/demos/src/Marks/Link/React/index.jsx
+++ b/demos/src/Marks/Link/React/index.jsx
@@ -16,65 +16,14 @@ export default () => {
       Paragraph,
       Text,
       Code,
-      Link.configure({
-        openOnClick: false,
-        autolink: true,
-        defaultProtocol: 'https',
-        protocols: ['http', 'https'],
-        isAllowedUri: (url, ctx) => {
-          try {
-            // construct URL
-            const parsedUrl = url.includes(':') ? new URL(url) : new URL(`${ctx.defaultProtocol}://${url}`)
+      Link.extend({
+        addOptions() {
+          console.log(this.parent)
+          return {
+            ...this.parent(),
 
-            // use default validation
-            if (!ctx.defaultValidate(parsedUrl.href)) {
-              return false
-            }
-
-            // disallowed protocols
-            const disallowedProtocols = ['ftp', 'file', 'mailto']
-            const protocol = parsedUrl.protocol.replace(':', '')
-
-            if (disallowedProtocols.includes(protocol)) {
-              return false
-            }
-
-            // only allow protocols specified in ctx.protocols
-            const allowedProtocols = ctx.protocols.map(p => (typeof p === 'string' ? p : p.scheme))
-
-            if (!allowedProtocols.includes(protocol)) {
-              return false
-            }
-
-            // disallowed domains
-            const disallowedDomains = ['example-phishing.com', 'malicious-site.net']
-            const domain = parsedUrl.hostname
-
-            if (disallowedDomains.includes(domain)) {
-              return false
-            }
-
-            // all checks have passed
-            return true
-          } catch (error) {
-            return false
-          }
-        },
-        shouldAutoLink: url => {
-          try {
-            // construct URL
-            const parsedUrl = url.includes(':') ? new URL(url) : new URL(`https://${url}`)
-
-            // only auto-link if the domain is not in the disallowed list
-            const disallowedDomains = ['example-no-autolink.com', 'another-no-autolink.com']
-            const domain = parsedUrl.hostname
-
-            return !disallowedDomains.includes(domain)
-          } catch (error) {
-            return false
           }
         },
-
       }),
     ],
     content: `
diff --git a/packages/core/src/Mark.ts b/packages/core/src/Mark.ts
index 6893bfce2..65c21afb3 100644
--- a/packages/core/src/Mark.ts
+++ b/packages/core/src/Mark.ts
@@ -550,7 +550,10 @@ export class Mark<Options = any, Storage = any> {
     defaultOptions: {},
   }
 
-  constructor(config: Partial<MarkConfig<Options, Storage>> = {}) {
+  constructor(config: Partial<MarkConfig<Options, Storage>> = {}, parent: Mark | undefined = undefined) {
+    if (parent) {
+      this.parent = parent
+    }
     this.config = {
       ...this.config,
       ...config,
@@ -608,7 +611,7 @@ export class Mark<Options = any, Storage = any> {
   extend<ExtendedOptions = Options, ExtendedStorage = Storage>(
     extendedConfig: Partial<MarkConfig<ExtendedOptions, ExtendedStorage>> = {},
   ) {
-    const extension = new Mark<ExtendedOptions, ExtendedStorage>(extendedConfig)
+    const extension = new Mark<ExtendedOptions, ExtendedStorage>(extendedConfig, this)
 
     extension.parent = this
 
diff --git a/packages/extension-link/src/link.ts b/packages/extension-link/src/link.ts
index b9b882995..e5ad5232d 100644
--- a/packages/extension-link/src/link.ts
+++ b/packages/extension-link/src/link.ts
@@ -237,6 +237,7 @@ export const Link = Mark.create<LinkOptions>({
   },
 
   addOptions() {
+    console.log('original options', this.parent)
     return {
       openOnClick: true,
       linkOnPaste: true,

To set the correct parent in the constructor. This would be a good thing to fix, looks like options were not being handled properly

For my own education, why the hesitation for this approach? After all, it seems like the implementation already handles undefined safely. Seems like this type change would also be “more correct” & bring the types & runtime closer together?

Sure mergeAttributes could be more forgiving & that would be correct to it’s implementation. But, it is masking over the real issue, that this.options is always defined for an .extend extension. So, making it possibly null enforces type checks everywhere it is referenced which also “breaks” types for people. This affects options & storage when they should always be available