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.
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)
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,
addOptionsis 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:
To set the correct parent in the constructor. This would be a good thing to fix, looks like options were not being handled properly
Sure mergeAttributes could be more forgiving & that would be correct to it’s implementation. But, it is masking over the real issue, that
this.optionsis always defined for an.extendextension. So, making it possibly null enforces type checks everywhere it is referenced which also “breaks” types for people. This affectsoptions&storagewhen they should always be available