TypeScript definitions?

Posted on
  • I've put the Espruino Pico and Wifi through some paces and have to say I'm very pleased with these boards. Kudos!

    I find I'm much more productive coding in TypeScript rather than plain JavaScript, but can't find any TypeScript definitions.

    1. Has anybody created TypeScript definitions for the Espruino libraries?
    2. If so, where might they be found?
    3. If so, how might I contribute?

    If there are no definitions, I'd be happy to contribute.

    Thank you.

  • Hi,

    I'm afraid there aren't any definitions around, although it would be easy enough to extract the information from the Espruino build process. All the functions are described in 'wrapper files': https://github.com/espruino/Espruino/blo­b/master/README_BuildProcess.md#wrapper-­files-

    For instance there's a type file for Tern's autocomplete in the Web IDE: http://www.espruino.com/json/espruino.js­on

    However that doesn't contain all the info - for instance if you wrote your own tool to look at the JSON comments in the wrappers, you could determine what was definitely supposed to be an integer/float/etc.

    Having said all that, have you tried running TypeScript-compiled code yet? There was an issue posted up a week or so ago - in that case the TypeScript compiler that was being used created code where functions are used before they're declared.

    That's fine in normal JS where everything is parsed beforehand, but in Espruino it's parsed as it is executed, so 'Function Hoisting' doesn't work. To get code working in that case you'd ideally need to flip some setting in the transpiler to make it generate code for functions in an order that doesn't rely on the hoisting.

  • I use Espruino with TypeScript and was a bit disappointed at the time that the Typescript goal of the last campaign was not reached.

    So I tried to generated a file espruino.d.ts by myself but gave up. It's seems not possible to generate good TypeScript definitions from the other type files (e.g. espruino.json). There are more informations required than available:

    // This class should be automatically generated.
    class I2C {
            // ...
            readFrom( address: number | { address: number, stop: boolean }, quantity: number ): Uint8Array;
    }
    

    But the corresponding jswrap comment is

    /*JSON{
      "type" : "method",
      "class" : "I2C",
      "name" : "readFrom",
      "generate" : "jswrap_i2c_readFrom",
      "params" : [
        ["address","JsVar","The 7 bit address of the device to request bytes from, or an object of the form `{address:12, stop:false}` to send this data without a STOP signal."],
        ["quantity","int32","The number of bytes to request"]
      ],
      "return" : ["JsVar","The data that was returned - as a Uint8Array"],
      "return_object" : "Uint8Array"
    }
    Request bytes from the given slave device, and return them as a Uint8Array (packed array of bytes). This is like using Arduino Wire's requestFrom, available and read functions.  Sends a STOP
     */
    

    The important information of parameter address is only in the description. :-(

    Currently it is required to write the TypeScript type definition by hand but there is always the risk that the type file will become outdated.

    @Gordon: Do you have an idea how it can go on in the long term?

  • Potentially we could look at adding some extra fields to the JSON that allow the types to be documented in a more typescript-friendly way? However surely you don't have to describe every API function at that level?

    I mean, even without that it's still going to be able to check your code and your use of the API functions in a lot of cases (just not all of them).

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

TypeScript definitions?

Posted by Avatar for sfl @sfl

Actions