• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Module Development in TypeScript

Scheduled Pinned Locked Moved Development
6 Posts 5 Posters 1.3k Views 5 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    Jalibu Module Developer
    last edited by Jalibu Oct 11, 2021, 9:09 AM Oct 11, 2021, 8:53 AM

    Hello developers,

    some time ago I switched all my modules to TypeScript (feel free to check out MMM-Jast as a reference implementations).
    Especially for more complex modules, TypeScript brings huge advantages from my point of view.
    Since the MagicMirror platform doesn’t provide its own TypeDefinitions, I made them available via the DefinitelyTyped library.
    For interested developers I also plan to provide some kind of TypeScript template module soon.

    .
    ├── src
    │   └── backend
    │       ├── Backend.ts
    │       ├── BackendUtils.ts
    │       └── ...
    │   └── frontend
    │       ├── Frontend.ts
    │       ├── FrontendUtils.ts
    │       └── ...
    │   └── types
    │       ├── ABC.ts
    │       └── ...
    └── templates
    │       ├── main.njk
    │       └── ...
    └── translations
    │       ├── de.json
    │       ├── en.json
    │       └── ...
    └── MMM-XYZ.js (generated)
    └── node_helper.js (generated)
    └── MMM-XYZ.css
    └── tsconfig.json
    └── rollup.config.json
    └── README.md
    └── package.json
    

    Question: Are there any other developers among you who are interested in joining my “TypeScript” initiative? A little support from another developer in maintaining the type definitions would be helpful.

    S H 2 Replies Last reply Oct 13, 2021, 4:44 AM Reply Quote 2
    • S Offline
      strawberry 3.141 Project Sponsor Module Developer @Jalibu
      last edited by Oct 13, 2021, 4:44 AM

      @jalibu there is a simple version of types in the main repo https://github.com/MichMich/MagicMirror/blob/master/module-types.ts

      Please create a github issue if you need help, so I can keep track

      J 1 Reply Last reply Oct 13, 2021, 8:36 AM Reply Quote 0
      • J Offline
        Jalibu Module Developer @strawberry 3.141
        last edited by Oct 13, 2021, 8:36 AM

        @strawberry-3-141
        as you say, it is a very simple and incomplete version. And you can not install them as a package or use them in your modules, except if you copy the file.
        In fact, that’s the real reason I created the typings on DefinitelyTyped.
        You can find the sources here and the npm module here.

        1 Reply Last reply Reply Quote 0
        • H Offline
          Henrik @Jalibu
          last edited by Aug 30, 2023, 8:51 AM

          @Jalibu That is really cool, I was thinking about how typescript could be used when developing modules. Like you say, it has huge advantages.
          What is the current state of the “TypeScript initiative”? Did it get under way?

          1 Reply Last reply Reply Quote 0
          • D Offline
            drdeath
            last edited by Apr 6, 2024, 8:14 PM

            Thank you very much for your great work! That is exactly what I needed. Coming from stringently typed languages like C and Java, ever since I discovered Typescript I find plain Javascript increasingly tedious.

            1 Reply Last reply Reply Quote 0
            • F Offline
              FrostByte
              last edited by 9 days ago

              Hi @Jalibu,

              thanks for the typings.

              To be honest, it’s not the best possible integration. The MagicMirror repository includes a module-types.ts file, containing the most important types, which I extended a little to fit my needs. A deeper integration, like being able to get correct typings/autocompletion for the this object in functions, is not available. If I find myself using it more in the future, I may look into improving this.

              I want to share an little extension or may a solution for the problem quoted:

              import type { Config } from "../types/Config";
              import type { State } from "../types/State";
              
              interface FrontendModuleProperties
                extends Partial<Module.ModuleProperties<Config>> {
                state?: State;
              }
              
              Module.register<Config>("MMM-XX", {
                defaults: {
                  x: 29,
                  y: "abc",
                },
              
                getStyles() {
                  return ["z.css"];
                },
               <...>
              
                getTemplateData() {
                  return {
                    config: this.config,
                    value: this.state?.value,
                  };
                },
              
               <...>
              
                socketNotificationReceived(notificationIdentifier: string, payload: State) {
                  if (notificationIdentifier === `ABC-${this.identifier}`) {
                    const lastValue = this.state?.value;  // <-- Here lastValue got correct type automatically
                    <...>
                      this.state = payload;
                  }
                },
              } as FrontendModuleProperties);
              
              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Enjoying MagicMirror? Please consider a donation!
              MagicMirror created by Michael Teeuw.
              Forum managed by Sam, technical setup by Karsten.
              This forum is using NodeBB as its core | Contributors
              Contact | Privacy Policy