javascript.d.ts exports Blockly.Generator itself instead of its instance
See original GitHub issueDescribe 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: The same code using my plugin to fix this bug (expected behavior):
- Note that these are of my local project different from my repository to reproduce this error.
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:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
Yes this has been fixed in #4753
I’ve made a plugin to get a proper type declaration: https://github.com/yukiya383/blockly-plugin-typed-codegenerator.