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

Make matchers more strongly typed #4083

Closed
wants to merge 6 commits into from

Conversation

justinbhopper
Copy link
Contributor

This adds stronger type declarations for the Matchers and Selectors in the clipboard module. The intention was to remove most of the @ts-expect-errors in the file and to make it more clear what kind of nodes each matcher could possibly be used for. It also prevents mixing a matcher with the wrong kind of node.

This PR is not expected to change any runtime behavior.

const CLIPBOARD_CONFIG: [Selector, Matcher][] = [
type MatcherConfig =
| [TextSelector, TextMatcher]
| [ElementSelector, ElementMatcher]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's safe in ElementMatcher to consider the first parameter as Element instead of Node because TextSelector and ElementSelector both contains string as the part of the union.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note: I've revised the PR types once more, but I think your concerns are addressed because:

  • TextSelector maps to Node['TEXT_NODE'], so TextMatcher is only for Text nodes
  • ElementSelector maps to string | Node['ELEMENT_NODE'], so ElementMatcher is only for Element nodes
  • It is possible for a matcher to meet both the TextMatcher and ElementMatcher signature at the same time (see matchNewLine for an example). In this case, it defines itself as (node: Text | Element, ...) => ... so it can be used in either situation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@luin Let me know if you're still unsure about this change or whether you'd like time to consider it. I'm happy to close the PR if you think it's better in its current state.


const CLIPBOARD_CONFIG: [Selector, Matcher][] = [
type TextMatcher = (node: Text, delta: Delta, scroll: ScrollBlot) => Delta;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like in some cases this change makes things a bit harder. E.g. https://share.slab.com/2dv3PhtG, previously, the parameters have the correct typing, but with this change, TypeScript can't infer the types and require users to provide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct -- inference isn't really possible and that was actually intentional. The goal of the PR was that the matchers need to be strongly typed to prevent misconfiguration.

In your example, previously the parameters would have just simply been (node: Node, ...). While it is inferring the signature, it isn't protecting against mismatch.

Is type inference a requirement here? If so, then I would say the PR should be closed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is type inference a requirement here?

Yeah I think it is. One reason is that we aim to minimize the effort required from users when upgrading to version 2.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay no problem, then probably this PR would do more harm than good so I'll close it out!

@justinbhopper justinbhopper deleted the matcher-types branch April 5, 2024 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants