quill icon indicating copy to clipboard operation
quill copied to clipboard

Typescript Errors when Creating Custom Blots

Open gillycheesesteak opened this issue 1 year ago • 3 comments

When creating a custom blot, I ran into a confusing situation which I believe can be improved through more explicit documentation or better typescript definitions

Steps for Reproduction

First I tried like this

import Quill from 'quill';

const InlineBlot = Quill.import('blots/inline');

export default class MyCustomBlot extends InlineBlot {...

The return type from Quill.import is unknown, so typescript doesn't like this (even though it works when transpiled)

Then, thinking I needed to import the InlineBlot type directly, I saw that quill does not export types for its blots at the root, so I figured I needed to import from parchment directly

import { InlineBlot } from 'parchment';

export default class MyCustomBlot extends InlineBlot {...

This makes Typescript happy, but this causes issues because the InlineBlot from parchment is not equivalent to blots/inline from quill (this is confusing but I'm sure could be addressed with documentation). The longer explanation is that the custom blot ends up getting erased because of the way the optimize function works on the parent blot, and due to the custom blot extending the wrong subclass

I also tried casting the Quill.import but Typescript wasn't happy about that either

import Quill from 'quill';
import { InlineBlot } from 'parchment';

const Inline = Quill.import('blots/inline') as InlineBlot;

export default class MyCustomBlot extends Inline {...

Typescript gives an error something like Inline is not a constructor, which to be honest I don't understand fully.

Finally, I realized that I can import from the file in quill directly, and this works and satisfies typechecking.

import InlineBlot from 'quill/blots/inline';

export default class MyCustomBlot extends InlineBlot {...

In my opinion, the fact that the quill "enhanced" blots are not accessible as imports from the root of the module gives the impression that they are not meant to be used directly. So while it is not an issue per se, I am left wondering if the documentation or exports could be improved to make this extension feel more natural.

Also, if I am still not doing this the recommended way, an explanation of what that recommended way is would be appreciated and could also be added to documentation.

gillycheesesteak avatar May 26 '24 16:05 gillycheesesteak

Yes, your last example is the recommended way. We do have a documentation for it: link.

In my opinion, the fact that the quill "enhanced" blots are not accessible as imports from the root of the module gives the impression that they are not meant to be used directly

That's fair. I think it makes sense to export them in the root of the module.

luin avatar May 31 '24 10:05 luin

Cool, thank you for the link. I think maybe this doc could be updated to make it clear how to accomplish this with proper typechecking (maybe just by updating the Quill.import() to a direct import).

I'm also curious how you envision Quill.import working in general with Typescript in v2. It seems that directly importing might be the recommended approach going forward?

Thank you for your help!

gillycheesesteak avatar May 31 '24 20:05 gillycheesesteak

I'm also curious how you envision Quill.import working in general with Typescript in v2. It seems that directly importing might be the recommended approach going forward?

Quill.import() is designed to be used in the UMD build, and it's impossible to make it work with TypeScript perfectly due to it connects to a dynamic registry.

It seems that directly importing might be the recommended approach going forward?

Yes. Unless you are not using a bundler, directly importing is the recommended approach.

luin avatar Jun 01 '24 08:06 luin

Definitely agree with the OP in the confusion, in migrating from Quill v1 to v2 we were correcting our custom blots and the documentation (like the Cloning Medium with Parchment Guide) all referenced doing Quill.import. Even a small notice on the extending blots page would be helpful.

vkoves avatar Feb 19 '25 16:02 vkoves