Moddable-OpenSource / moddable

Tools for developers to create truly open IoT products using standard JavaScript on low cost microcontrollers.
http://www.moddable.com
1.34k stars 237 forks source link

Typings for `Time.microseconds` #1404

Closed cmidgley closed 2 months ago

cmidgley commented 2 months ago

Added microseconds to Time type definitions.

I spent too much time debating this implementation with myself! The simple solution is to just add it as another method and leave it up to the user to decide if they wish to use it or not (without any TS-level validation, as in microseconds(): number). A more complex solution would be to create a separate .d.ts file that extends Time and change the manifest to include it for the platforms that implement microseconds. But that turns into an ugly build problem for consumers of it - for example, code using microseconds for ESP32 could would fail to build on simulator requiring breaking apart source files and separate manifest files, when otherwise you could have done a runtime check.

I ended up marking it optional (microseconds?()), which with TS strict on would require you to check (e.g. if (Time.microseconds) ...) but without strict it will just allow the access. This seemed to most closely match how the JS implementation works.