Better type inference for transaction meta properties #27

Closed
nperez0111 wants to merge 1 commit from meta-typing into master
nperez0111 commented 2025-04-25 17:52:59 +02:00 (Migrated from github.com)

This PR adds better type inference for a transactions getMeta & setMeta methods.

It uses some TypeScript inference based on the fact that Plugin and PluginKey are already generically typed. This allows us to enforce that the shape of the meta property is properly inferred for more accurate type checking.

See the example below, given this code:

const pluginKey = new PluginKey<{inferred:true}>() 
const tr: Transaction = new Transaction(EditorState.create({}))
const meta = tr.getMeta(pluginKey)

It is correctly inferred:

image
This PR adds better type inference for a transactions `getMeta` & `setMeta` methods. It uses some TypeScript inference based on the fact that `Plugin` and `PluginKey` are already generically typed. This allows us to enforce that the shape of the meta property is properly inferred for more accurate type checking. See the example below, given this code: ```ts const pluginKey = new PluginKey<{inferred:true}>() const tr: Transaction = new Transaction(EditorState.create({})) const meta = tr.getMeta(pluginKey) ``` It is correctly inferred: <img width="539" alt="image" src="https://github.com/user-attachments/assets/88f1ccc6-a6b2-4428-b529-a0829c321ca7" />
marijnh commented 2025-04-25 19:05:55 +02:00 (Migrated from github.com)

Plugins and plugin keys are parameterized by the type of their plugin state. While in some cases you might use setMeta with this same type, there is nothing that requires this—you can put anything in meta fields, their key simply doesn't constrain the value type. As such, I don't think this patch is a good idea.

Plugins and plugin keys are parameterized by the type of their plugin state. While in some cases you might use `setMeta` with this same type, there is nothing that requires this—you can put anything in meta fields, their key simply doesn't constrain the value type. As such, I don't think this patch is a good idea.
nperez0111 commented 2025-04-25 22:34:14 +02:00 (Migrated from github.com)

Ah, you know, on further reflection, indeed is dumb.

Ah, you know, on further reflection, indeed is dumb.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
prosemirror/prosemirror-state!27
No description provided.