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.

javascript.d.ts exports Blockly.Generator itself instead of its instance

See original GitHub issue

Describe the bug Instead of using Blockly.JavaScript, we can import it from “blockly/javascript” in below statement:

import * as JavaScript from "blockly/javascript";

Then, we can use some useful functions such as:

const value = JavaScript.valueToCode(block, 'value', JavaScript.ORDER_ASSIGNMENT||16)||"";

It can be executed without error, but can’t pass type check, and error displayed in vscode.

To Reproduce Go to my repository to reproduce this error. Follow the steps written in README.md.

  • Note that the above repository includes other errors which remains after this bug fixed, see its README for more info about them.

Expected behavior JavaScript.valueToCode passes type check.

Screenshots A code using Blockly package: JavaScript_is_treated_as_typeof_Generator The same code using my plugin to fix this bug (expected behavior): JavaScript_is_treated_as_Generator

Stack Traces

src/block.ts:15:28 - error TS2339: Property 'valueToCode' does not exist on type 'typeof Generator'.

15   const value = JavaScript.valueToCode(block, 'value', JavaScript.ORDER_ASSIGNMENT||16)||"";

Estimated cause Copied from README of my repository to reproduce this error. Current contents of typings/javascript.d.ts are shown below:

import * as Blockly from './core';
export = Blockly.Generator;

This means that, when you import ‘blockly/javascript’, its type is treated as typeof Blockly.Generator. However, javascript.js exports Blockly.JavaScript, which is an instance of Blockly.Generator.

Suggested fix That type declaration should be changed to the one below:

import * as Blockly from './core';
declare var tmp:Blockly.Generator;
export = tmp;

to export an instance of Blockly.Generator instead of typeof Blockly.Generator.

Additional context You know, Blockly.JavaScript is not defined in blockly.d.ts, and therefore can’t be used in typescript project. Therefore, it is very preferable if we can directory import it with proper type. If type of JavaScript is properly set to Blockly.Generator, we can easily fix other type problems around it by extending Blockly.Generator properly and downcasting JavaScript to the extended class. However, if type of JavaScript is not properly set, we can’t do such hack easily: we must change blockly itself, or cast JavaScript to any to solve type errors. This is why I want this bug fixed.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
samelhusseinicommented, Jun 14, 2021

Yes this has been fixed in #4753

1reaction
yukiya383commented, Mar 31, 2021

I’ve made a plugin to get a proper type declaration: https://github.com/yukiya383/blockly-plugin-typed-codegenerator.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generating and Running JavaScript | Blockly
Blockly applications often generate JavaScript as their output language, generally to run within a web page (possibly the same, ...
Read more >
TypeScript generates .d.ts file with defines declarations ...
Why TypeScript generates a file that is then not working with TypeScript itself? Why is it using define instead of export ? (if...
Read more >
July 2021 Release is Available! - Google Groups
javascript.d.ts exports Blockly.Generator itself instead of its instance (#4742). - Add an option allowing to select and drag multiple blocks at the same ......
Read more >
blockly - UNPKG
507, * Used to match connections between a block and its insertion marker. 508, * @param {!Blockly.Block} otherBlock The other block to match...
Read more >
MakeCode Languages: Blocks, Static TypeScript and Static ...
TypeScript itself is a superset of JavaScript, and many MakeCode programs, ... are specific to JavaScript (for example, prototype inheritance), and instead ......
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