-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Feature: Ensure the type passed to createCommand
is retained, inferred, and enforced
#2536
Comments
Thanks for these @PAkerstrand. Your working solution to 2 seems really strong. We used to have payload to command type inference when we used Flow, but we must have lost that when we auto-generated the new TypeScript types. If you have type issues by changing any of these to union type in any of the editor.registerCommand(
CUT_COMMAND,
(event) => {
const selection = $getSelection();
if (!$isRangeSelection(selection)) {
return false;
}
if (event instanceof ClipboardEvent) {
onCutForPlainText(event, editor);
}
return true;
},
COMMAND_PRIORITY_EDITOR,
), The other issue you identified in 1. is definitely on my radar as it will affect our ability to add command logging as part of #2393. Sadly, I'm actually not sure if there's a way around it without changing the existing API. We don't want to require the developer to have to pass a Still not sure of a solution, but I will keep thinking. |
Thanks for the feedback @thegreatercurve 👌🏼 Yeah, I agree that the solution to nr. 1 isn't really clear. I guess it would be possible to use some source transformations to add a string property to the command itself, based on the value of the variable the command is stored in. That would, however, require a transformation step. Another solution would be to not use an object, but instead a Anyway, I'll polish up the PoC PR a bit, and let's take it from there |
@thegreatercurve I'm having some issues with various events not being available in the unit tests. I guess this is down to the testing environment in Jest and what's exposed by the
Any ideas whether it is fixable and how to set it up? |
@PAkerstrand Interesting. I think this might be a bug within Jest or JSDOM. From a quick experiment, it works the other way around if you do the check as That should be enough to overcome the CI issues. |
Closed by #2537 |
Issue created from a Q-and-A post over at Discord: https://discord.com/channels/953974421008293909/955972012541628456/990966752110325771
I really dig the command system in Lexical, but there are two things that irks me a little bit:
Strongly typing the command seems like a great idea, but the underlying types makes it a bit hard to debug the code. Basically,
createCommand()
-call just creates an empty object{}
that's used as a unique key for aMap
. This is great, but given that approach, whenever a command is dispatched, thetype
is simply{}
. Would it make sense to have some internal field or name to trace which command is being dispatched?Each
LexicalCommand
is generic in terms of the payload, so you can have aLexicalCommand<boolean>
, orLexicalCommand<KeyboardEvent>
. However, theLexicalCommand<T>
is really just an alias forReadonly<Record<string, unknown>>
, so the required type of the payload isn't saved. This also makes us lose type safety down the line, where you can doand TypeScript will happily accept it. It'd be good if the payload was constrained by the type of the command here.
Additionally, when you
registerCommand
you need to repeat the type of the payload. Given that the type information was stored inside of the command itself, this could then be inferred, along the lines of:Finally, if the
registerCommand
could infer the payload type from the command itself, then we could ensure that the command handler has the expected signature. With the current approach there is nothing preventing a very similar error as in thedispatchCommand
example, where theregisterCommand
-handler expects a payload differing from the one provided tocreateCommand
.As a starting point to the second bullet, I've created a PoC draft PR. There are still some type checking errors introduced by this change, related to
LexicalEvents
:The text was updated successfully, but these errors were encountered: