question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript definition problems

See original GitHub issue

The current blockly.d.ts TypeScript definition file does not accurately describe the data types in many places of the API, either through errors in the JSDoc types, insufficiently rich typing, or just incompatibilities between Closure and TS.

I’m currently upgrading our codebase to use Blockly July 2019 release, from its npm package rather than directly from the repo, and the supplied TypeScript definitions, rather than our very outdated and hacked definitions.

I’m opening this issue to start documenting the areas in which I’ve had to patch up or workaround issues in the supplied blockly.d.ts file…

  • Mismatch between Blockly.Options and BlocklyOptions - the inject fn takes the former, but really should take the latter
  • A BlockSvg should have a workspace of type WorkspaceSvg
  • BlockSvg.render is missing
  • WorkspaceSvg.newBlock should return a BlockSvg
  • Events.Abstract should ideally have a type property, rather than individual subclasses having it
  • Icon is missing some internal props and methods, which makes adding a custom Icon a pain

I’ve managed to fix above with the following…

import Blockly from 'blockly'

declare module 'blockly' {

    interface BlockSvg {
        workspace: Blockly.WorkspaceSvg

        render(opt_bubble?: boolean): void
    }

    interface Options extends Blockly.BlocklyOptions {
    }

    interface WorkspaceSvg {
        newBlock(prototypeName: string, opt_id?: string): Blockly.BlockSvg
    }

    interface Icon {
        block_: Blockly.BlockSvg
        iconGroup_?: SVGElement

        drawIcon_(group: SVGElement): void

        setVisible(visible: boolean): void
    }

    namespace Events {
        interface Abstract {
            type: string
        }
    }
}
  • Blockly.Msg is declared as a module with a single LOGIC_HUE prop, preventing arbitrary overrides within TS code, really it should be declared as a Record<string, string> to allow any message key to be overridden. The only work-around I’ve found for this is use of // @ts-ignore 😦
  • Blockly.VariableMap.prototype.getVariable returns Blockly.VariableModel but should be Blockly.VariableModel | null according to the docs

more to come…

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
samelhusseinicommented, Dec 11, 2020

Adding more to this. The different renderers we publish are currently not included in the blockly.d.ts

1reaction
jollytoadcommented, Sep 14, 2020

Hey gang, finally getting round to another Blockly update, from 3.20200123.1 to 3.20200625.2, so I thought I should keep this ticket updated.

Here are my type fixes for this release…

import 'blockly'

declare module 'blockly' {

    interface BlocklyOptions {
        parentWorkspace?: Blockly.WorkspaceSvg
    }

    interface WorkspaceSvg {
        newBlock(prototypeName: string, opt_id?: string): Blockly.BlockSvg
    }

    interface Icon {
        iconGroup_?: SVGElement

        setVisible(visible: boolean): void
    }

    module fieldRegistry {
        function register(type: string, fieldClass: { fromJson: (options: any) => Blockly.Field }): void
    }

    namespace Events {
        interface Abstract {
            type: string
        }
    }
}

The most notable change being the Blockly.fieldRegistry.register fieldClass param appears to be declared incorrectly…

The blockly.d.ts declares register as:

function register(type: string, fieldClass: { (_0: any[]): any /*missing*/ }): void;

but fieldClass should be an object containing a fromJson function rather than just a function.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problems with TypeScript - Hacker News
TypeScript's major weakness is that it doesn't want to break away from Javascript. That strict dedication to being a superset means the type ......
Read more >
Problems With TypeScript in 2020 - Execute Program
When used as a long-lived watcher process, TypeScript has broken this trend: it's buggy. The compiler tends to hit bugs when files are...
Read more >
TypeScript: JavaScript With Syntax For Types.
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. Try TypeScript Now. Online or...
Read more >
TypeScript Done Wrong - OpenReplay Blog
So what's the problem then? As it turns out, the private keyword, which you'd use to define a private property in your class,...
Read more >
7 really good reasons not to use TypeScript - everyday.codes
Any network calls, system libraries, platform-specific APIs and non-typed third-party libraries have no way of communicating with TypeScript. As ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found