Using setSelection at the end of chained transactions

I currently have a chained transaction that “moves” content into a new container node elsewhere in the document. This “move” transaction works as expected. The transaction looks like this:

let tr = state.tr.insert(posToInsertAt, newExchangeNode)
  .delete(deleteFrom, deleteTo)
if (dispatch) dispatch(tr)

What I want to do is add a .setSelection() transaction so the cursor is at the beginning of moved content.

I’ve tried this:

    let tr = state.tr.insert(posToInsertAt, newExchangeNode)
      .delete(deleteFrom, deleteTo)
    let newSelection = tr.doc.resolve(newSelectionPos)
    tr = tr.setSelection(newSelection, newSelection)
    if (dispatch) dispatch(tr)

However, I get “Uncaught TypeError: Cannot read property ‘doc’ of undefined” on the setSelection line.

You’re passing a ResvoledPos where the setSelection method expects a Selection object. Try new TextSelection(...)