Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Typescript interfaces for toolbox and block definitions?

29 views
Skip to first unread message

Bart Zuidgeest

unread,
Jan 6, 2025, 11:43:11 AMJan 6
to Blockly

Hello,

Maybe it's just me. As I am mostly a c# developer...

But how do I type the json object array's I use to define blocks and toolbox items? I cannot seem to find any interfaces? Array<unknown> like below seems suboptimal.

The basic documentation seems to mention little about typescript. Or at least not in a from a typescript amateur like me understands.

const generatedBlock: Array<unknown> = [
    {
        type: 'RuleProcessor.Statements.BranchStatement',
        message0: 'Branch\r\n Condition %1 TrueLabel %2 FalseLabel %3',
        colour: 231,
        args0: [
            {
                name: 'Condition',
                type: 'input_value',
                check: ['IValueExpression<Boolean>'],
            },
            {
                name: 'TrueLabel',
                type: 'input_value',
                check: ['IValueExpression<String>'],
            },
            {
                name: 'FalseLabel',
                type: 'input_value',
                check: ['IValueExpression<String>'],
            },
        ],
        previousStatement: null,
        nextStatement: null,
        inputsInline: false,
    },

Christopher Allen

unread,
Jan 7, 2025, 11:55:13 AMJan 7
to blo...@googlegroups.com
Hi Bart,

Maybe it's just me. As I am mostly a c# developer...

But how do I type the json object array's I use to define blocks and toolbox items? I cannot seem to find any interfaces? Array<unknown> like below seems suboptimal.

Blockly was originally written in pure JavaScript with Closure type annotations; when we migrated the codebase to TypeScript many of these were automatically converted to TypeScript types and many others we were able to convert manually.  In many cases, however, types of certain variables, properties and function arguments remain underspecified.  At the moment, the formal type for JSON block definitions is any[] (written in our codebase as AnyDuringMigration[], to remind us that we should at some point come back and improve it).

In general I appreciate that having more detailed type information would be helpful—particularly to those who use IDEs that make extensive use of the TypeScript language server—but in general we think the time is better invested in fixing bugs and improving our documentation.
 
The basic documentation seems to mention little about typescript. Or at least not in a from a typescript amateur like me understands.

There are two main reasons for this:
  • The preponderance of our documentation predates the migration to TypeScript.
  • The Blockly library is intended to be used from any JavaScript  program, so providing TypeScript-specific code snippets is less useful than providing pure JavaScript snippets.

const generatedBlock: Array<unknown> = [

It is completely OK to omit the type declaration here; the TypeScript compiler will infer a suitable type based on the initialiser.

If you were defining a mutable variable and you wanted to be able to change its value then either unknown[] or any[] would be suitable, depending on whether you wanted to oblige yourself to formally cast the value before using it.

Best wishes,

Christopher 

Bart Zuidgeest

unread,
Jan 7, 2025, 2:19:11 PMJan 7
to Blockly
Thank you for explaining.

I did read about your migration to typescript. That's why it struck me as weird that for all that effort, such "important" types were not specified. So I expected to be missing something.

I can't argue with your priorities and I do appreciate the effort and time that has gone in to this library.
Reply all
Reply to author
Forward
0 new messages