simulation icon indicating copy to clipboard operation
simulation copied to clipboard

Adding Typescript declarations?

Open filipesmedeiros opened this issue 5 months ago • 6 comments

Hey! :)

Are you perhaps interested in adding Typescript declarations or, even, migrating the repo to Typescript? It is not that big, so I think it could be done? You even already have the JSDoc declarations eheh

Thanks in advance!

filipesmedeiros avatar Sep 08 '25 11:09 filipesmedeiros

As an added note, this would make it easier to eventually port this library to AssemblyScript, so we can compile this down to WASM so it can run fast :)

filipesmedeiros avatar Sep 10 '25 12:09 filipesmedeiros

Hi Filipe,

The simulation package has extensive TypeScript type annotations using TypeScript's JSDoc support (https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html).

Type checking should work out of the box when imported into a TypeScript file and can be enabled in JavaScript files in editors that support it (like VSCode) via a // @ts-check annotation or project-wide with a jsconfig.json file (https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html).

If you are interested in performance optimizations, there are things that would likely have a larger impact and also be simpler than porting to a different language. For instance, implementing an adaptive step differential equation solver would be a great project. The simulation package uses a task scheduler for its solvers, so the foundation is already there for dynamic step sizes.

scottfr avatar Sep 13 '25 07:09 scottfr

Type checking should work out of the box when imported into a TypeScript file

Hmm it isn't for me, but that be my fault with setup I have (default Next.js with TS).

As for performance, that would indeed be cool to implement :) I have one some adaptive-step solvers in Julia, but have no clue how to implement them, though. It can a future project.

filipesmedeiros avatar Sep 13 '25 13:09 filipesmedeiros

Thanks for checking. I took a look and the type information doesn't work with the Next starter for me either.

Can you try navigating to the simulation folder in your node_modules and run:

npx -p typescript tsc src/**/*.js --declaration --allowJs --emitDeclarationOnly --outDir types

(https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html)

And then change the exports key of the simulation package.json to:

  "exports": {
    ".": {
      "types": "./types/api/Model.d.ts",
      "import": "./src/api/Model.js",
      "default": "./src/api/Model.js"
    }
  },

Let me know if that fixes it for you.

scottfr avatar Sep 13 '25 17:09 scottfr

@scottfr it did fix it :) Thank you!

filipesmedeiros avatar Sep 15 '25 16:09 filipesmedeiros

Glad to hear it! I'll add a version of this to the next release.

scottfr avatar Sep 20 '25 10:09 scottfr