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

Improve props argument type in Transforms.setNodes() #4638

Merged
merged 3 commits into from
Nov 4, 2021

Conversation

e1himself
Copy link
Contributor

@e1himself e1himself commented Oct 29, 2021

Description

Because Typescript can know which type of nodes we are modifying thanks to the T type inferred from the match function, if available, it can also properly narrow down the props argument type.

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn fix.)
  • The relevant examples still work. (Run examples with yarn start.)
  • You've added a changeset if changing functionality. (Add one with yarn changeset add.)

Because Typescript can know which type of nodes we are modifying thanks to the `T` inferred from `match` function, 
it can also properly narrow down the `props` argument type.
@changeset-bot
Copy link

changeset-bot bot commented Oct 29, 2021

🦋 Changeset detected

Latest commit: 0a54efd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
slate Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@e1himself e1himself changed the title Fix props argument type in Transforms.setNodes() Improve props argument type in Transforms.setNodes() Oct 29, 2021
@e1himself
Copy link
Contributor Author

e1himself commented Nov 3, 2021

Is there anything I should do additionally to submitting a PR to get this viewed by someone? Like pinging a maintainer? :)

Didn't find anything specific to that in the contribution guide.

@dylans
Copy link
Collaborator

dylans commented Nov 4, 2021

Is there anything I should do additionally to submitting a PR to get this viewed by someone? Like pinging a maintainer? :)

Didn't find anything specific to that in the contribution guide.

Apologies, just a busy week. I had reviewed it when it was first submitted and it looked good to me, but I tend to leave things open for a few days in case others on the team want to chime in. Going to accept this now and it will be in the next release. Thanks for the contribution!

@dylans dylans merged commit e0f4151 into ianstormtaylor:main Nov 4, 2021
@github-actions github-actions bot mentioned this pull request Nov 4, 2021
@e1himself e1himself deleted the patch-1 branch November 4, 2021 14:31
e1himself added a commit to prezly/slate that referenced this pull request Nov 12, 2021
They are redundant now after the `Node.setNodes()` patch
has been released.

@see ianstormtaylor/slate#4638
e1himself added a commit to prezly/slate that referenced this pull request Nov 12, 2021
They are redundant now after the `Node.setNodes()` patch
has been released.

@see ianstormtaylor/slate#4638
@htulipe
Copy link
Contributor

htulipe commented Nov 26, 2021

This PR breaks some valid use case like switching from a bullet to a numbered list:

Transforms.setNodes(
      editor,
      { type: ElementType.NumberedList },
      { match: isBulletedListElement }
    );
    
function isBulletedListElement(
  node: Node
): node is BulletedListSlateElement {
  return Element.isElement(node) && node.type === ElementType.BulletedList;
}  

TS complains it can't set type to ElementType.NumberedList because it must be of type ElementType.BulletedList | undefined

@e1himself
Copy link
Contributor Author

e1himself commented Nov 26, 2021

@htulipe
I believe this can be handled by explicitly telling TS that you are okay switching node type:

Transforms.setNodes<BulletedListSlateElement | NumberedListSlateElement>(
      editor,
      { type: ElementType.NumberedList },
      { match: isBulletedListElement }
);
    
function isBulletedListElement(
  node: Node
): node is BulletedListSlateElement {
  return Element.isElement(node) && node.type === ElementType.BulletedList;
}  

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.

None yet

3 participants