blockly: TypeScript definition problems
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.OptionsandBlocklyOptions- the inject fn takes the former, but really should take the latter - A
BlockSvgshould have a workspace of typeWorkspaceSvg BlockSvg.renderis missingWorkspaceSvg.newBlockshould return aBlockSvgEvents.Abstractshould ideally have a type property, rather than individual subclasses having itIconis 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.Msgis declared as a module with a singleLOGIC_HUEprop, preventing arbitrary overrides within TS code, really it should be declared as aRecord<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.getVariablereturnsBlockly.VariableModelbut should beBlockly.VariableModel | nullaccording to the docs
more to come…
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 15 (8 by maintainers)
Adding more to this. The different renderers we publish are currently not included in the blockly.d.ts
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…
The most notable change being the
Blockly.fieldRegistry.registerfieldClassparam appears to be declared incorrectly…The blockly.d.ts declares
registeras:but
fieldClassshould be an object containing afromJsonfunction rather than just a function.