Skip to content

TypeScript baselining (and various things) #1434

@ScreamZ

Description

@ScreamZ

Foreword

Hi Moddable team,
this is my first contribution to this awesome project so I want first to thank you for your awesome work! The tool you made so far is really awesome and I'm pretty sure this could be an industry game changer like React Native did on it's own. Projects are really similar in terms of concepts and I'm pretty sure we can take inspiration for various things for Expo https://expo.dev/ in terms on how they work for the developer experience and things.

Damn this is soooo cool to write JavaScript to program my MCU, I was dying with C/C++ restriction as a 10 years+ JS developer.

That being said, I'm working in conjunction with @HipsterBrown on xs-dev to try to improve the developer experience and the onboarding on the moddable SDK. The technology is awesome and the big brake on adoption is the learning curve and documentation that is very split on many side and not always up-to-date.

State of art

Currently I'm working on having a minimal full-typescript example with multiple module management that work on the stack. I've identified some issues and things that are being tracked here HipsterBrown/xs-dev#186.

I wanted to open this topic so we can discuss about current issues on the TypeScript typings and how we could improve that, so let's start.

Here are so postulates:

  • Most of the tools/library exposed by Moddable SDK are provided as what I would call « Build Injection », meaning they are injected on build time based on the manifest.json.
  • The ideal goal would be to add them as library managed by the package manager, as I said earlier you can take a look at expo components, but I agree this would be a lot of work, so for the first draft we can keep the first postulate that they remain injected on build. If so, each package would expose its own typings, like regular package using the types property of its package.json
  • Typings are provided as an independent package, not hosted on https://github.com/DefinitelyTyped/DefinitelyTyped

So what could be expected?

Starting my prototyping, it was clear that typings once installed with any package manager (in my case pnpm but could be any) are not loaded by default. This is because Typescript only resolve package-free typings from a node_modules/@types folder. And this is the folder for dependencies managed by https://github.com/DefinitelyTyped/DefinitelyTyped.

A first proposal could be to host those typings here. But we can work around.

The second proposal and the correct way to manage this could be to use the typesRoot property of the tsconfig.json file just like so:

{
  "compilerOptions": {
    "typeRoots": ["./node_modules/@moddable/typings", "./node_modules/@types"],
  }
}

But sadly, with the current state of art, the ts compiler yield an error

Screenshot 2024-11-15 at 10 56 26

Therefore the only way to proceed at the moment is doing so (with an example from my repo)

{
  "compilerOptions": {
    "incremental": true,
    "outDir": "dist",
    "lib": ["es2022"],
    "module": "es2022",
    "sourceMap": true,
    "target": "ES2022",
    "types": [
      "./node_modules/@moddable/typings/xs",
      "./node_modules/@moddable/typings/mcpack",
      "./node_modules/@moddable/typings/websocket",
      "./node_modules/@moddable/typings/wifi",
      "./node_modules/@moddable/typings/global",
      "./node_modules/@moddable/typings/embedded_provider/builtin",
      "./node_modules/@moddable/typings/embedded_io/digital"
    ]
  },
  "include": ["src/**/*.ts", "src/main.ts"]
}

The issue is that you need to specify all files manually, and then all deps of required file, example for the global typings, i had to add embedded_provider/builtin and embedded_io/digital.

This get a bit cumbersome.

What's next

I just wanted to land this topic, and discuss about you if you're planning anything already in the pipe about this, or if we can help on improving the workflow about that.

Thanks,
Andréas

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions