1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-01-18 05:59:13 +01:00

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

* Fix `setNodes()` props argument type

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.

* Fix TS errors in examples

* Add a changeset
This commit is contained in:
Ivan Voskoboinyk 2021-11-04 13:43:18 +02:00 committed by GitHub
parent 9e8d5e2b9b
commit e0f41514a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 5 deletions

View File

@ -0,0 +1,5 @@
---
'slate': patch
---
Improve typescript type of `props` argument of `Transforms.setNodes()`

View File

@ -67,7 +67,7 @@ export interface NodeTransforms {
) => void
setNodes: <T extends Node>(
editor: Editor,
props: Partial<Node>,
props: Partial<T>,
options?: {
at?: Location
match?: NodeMatch<T>

View File

@ -73,7 +73,9 @@ const VideoElement = ({ attributes, children, element }) => {
const newProperties: Partial<SlateElement> = {
url: val,
}
Transforms.setNodes(editor, newProperties, { at: path })
Transforms.setNodes<SlateElement>(editor, newProperties, {
at: path,
})
}}
/>
</div>

View File

@ -37,7 +37,9 @@ const withLayout = editor => {
const enforceType = type => {
if (SlateElement.isElement(child) && child.type !== type) {
const newProperties: Partial<SlateElement> = { type }
Transforms.setNodes(editor, newProperties, { at: childPath })
Transforms.setNodes<SlateElement>(editor, newProperties, {
at: childPath,
})
}
}

View File

@ -67,7 +67,7 @@ const withShortcuts = editor => {
const newProperties: Partial<SlateElement> = {
type,
}
Transforms.setNodes(editor, newProperties, {
Transforms.setNodes<SlateElement>(editor, newProperties, {
match: n => Editor.isBlock(editor, n),
})

View File

@ -74,7 +74,7 @@ const toggleBlock = (editor, format) => {
const newProperties: Partial<SlateElement> = {
type: isActive ? 'paragraph' : isList ? 'list-item' : format,
}
Transforms.setNodes(editor, newProperties)
Transforms.setNodes<SlateElement>(editor, newProperties)
if (!isActive && isList) {
const block = { type: format, children: [] }