1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-02 11:42:53 +02: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
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 ) => void
setNodes: <T extends Node>( setNodes: <T extends Node>(
editor: Editor, editor: Editor,
props: Partial<Node>, props: Partial<T>,
options?: { options?: {
at?: Location at?: Location
match?: NodeMatch<T> match?: NodeMatch<T>

View File

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

View File

@@ -37,7 +37,9 @@ const withLayout = editor => {
const enforceType = type => { const enforceType = type => {
if (SlateElement.isElement(child) && child.type !== type) { if (SlateElement.isElement(child) && child.type !== type) {
const newProperties: Partial<SlateElement> = { 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> = { const newProperties: Partial<SlateElement> = {
type, type,
} }
Transforms.setNodes(editor, newProperties, { Transforms.setNodes<SlateElement>(editor, newProperties, {
match: n => Editor.isBlock(editor, n), match: n => Editor.isBlock(editor, n),
}) })

View File

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