Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

typescript: type of event on workspace?

40 views
Skip to first unread message

Seb Seb

unread,
Jan 24, 2025, 5:20:03 PMJan 24
to Blockly
Hello,
working on using Blockly in pure typescript, I had a problem that eslint detected. Here's the problem in your function (https://developers.google.com/blockly/guides/configure/web/events):
function onFirstComment(event) { if (event.type == Blockly.Events.BLOCK_CHANGE && event.element == 'comment' && !event.oldValue && event.newValue) { alert('Congratulations on creating your first comment!') workspace.removeChangeListener(onFirstComment); } } workspace.addChangeListener(onFirstComment);
function onFirstComment(event) {
if (event.type == Blockly.Events.BLOCK_CHANGE && event.element == 'comment' && !event.oldValue && event.newValue) { alert('Congratulations on creating your first comment!') workspace.removeChangeListener(onFirstComment); } } workspace.addChangeListener(onFirstComment);

My IDE shows this error:
Parameter 'event' implicitly has an 'any' type.ts(7006)

And I don't know how to type it correctly...nor "Blockly.Events" or "Blockly.Events.abstract" as it suggests it.
Thanks for your help.

Mark Friedman

unread,
Jan 24, 2025, 5:47:56 PMJan 24
to blo...@googlegroups.com
You were so close!  You should be able to use Blockly.Events.Abstract as the type.

Hope this helps!

-Mark


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/blockly/c4a3708f-0f4e-436c-a9be-0c31c2bd189fn%40googlegroups.com.

Seb Seb

unread,
Jan 25, 2025, 6:16:10 AMJan 25
to Blockly
I already tried, but if I do it, I have a:
Property 'element' does not exist on type 'Abstract'.ts(2339)

Mark Friedman

unread,
Jan 25, 2025, 7:36:27 PMJan 25
to blo...@googlegroups.com
Yes, choosing the right type for the event parameter doesn't solve all the type issues.  You will also need to cast the event to the right specific type (i.e. Blockly.Events.BlockChange in this case).  Something like the following will satisfy the TypeScript type checker:

  function onFirstComment(event: Blockly.Events.Abstract) {
    if (event.type == Blockly.Events.BLOCK_CHANGE) {
      const blockChangeEvent = event as Blockly.Events.BlockChange;
      if (blockChangeEvent.element == 'comment' &&
          !blockChangeEvent.oldValue && blockChangeEvent.newValue) {

        alert('Congratulations on creating your first comment!')
        ws!.removeChangeListener(onFirstComment);
      }
    }
  }

I will also note that, while the above type checks ok, it doesn't appear to actually work to alert on the first comment in the code.  It seems that something has changed since that example in the documentation was created in which events are raised and what their properties are when comments are created.  This is true whether you use the JavaScript or TypeScript code.

-Mark


Seb Seb

unread,
Jan 26, 2025, 6:05:23 PMJan 26
to Blockly
Thanks, it works.
Reply all
Reply to author
Forward
0 new messages