How to create a custom language generator using TypeScript

132 views
Skip to first unread message

Вадим Лешкевич

unread,
Jul 8, 2021, 9:01:35 AM7/8/21
to Blockly
Hi there
I created a file customlng.d.ts

  import 'blockly/blockly.d.ts';

  declare module 'blockly' {
    class CustomLng extends Generator {}
  }

but when i create a new generatot in customlng.ts
   Blockly.CustomLng = new Blockly.Generator('CustomLng');
I get an error " Type 'Generator' is missing the following properties from type 'typeof CustomLng': prototype, NAME_TYPE  "

thanks

Beka Westberg

unread,
Jul 10, 2021, 10:53:12 AM7/10/21
to Blockly
Hello,

This looks like a simple type error =) It seems like the code is trying to assign a supertype value to a subtype variable.

For example, let's say you have a type Dog which is a subtype of Animal. You can't assign an Animal value to a Dog variable because Animal is missing some properties of Dog. For example, Animal can't necessarily bark or lick or wag a tail. So if you tried to call .bark() on that Animal value, it would throw an error. Therefor, the compiler doesn't allow you to do this (that's the joy of static typing!)

Now let's say you have a CustomLng which is a subtype of Generator. You can't assign a Generator value to a CustomLng value because Generator is missing some properties of CustomLng. For example, it seems like Generator is missing a NAME_TYPE property. So if you tried to call .NAME_TYPE on that Generator value, it would throw an error. Therefor the compiler doesn't allow you to do this.

I hope that helps! If you have any further questions please reply :D
--Beka
Reply all
Reply to author
Forward
0 new messages