petersalomonsen / javascriptmusic

A.K.A. WebAssembly Music. Live coding music and synthesis in Javascript / AssemblyScript (WebAssembly)
https://petersalomonsen.com
GNU General Public License v3.0
382 stars 32 forks source link

WIP add TypeScript #7

Open trusktr opened 4 years ago

trusktr commented 4 years ago

WIP, not to be merged yet, but we can use this PR to discuss and see progress.

There's no transpilation, only type checking and improved intellisense in VS Code or TypeScript-capable editors.

This is also a good process for me to get familiar with the code.

This PR will focus only on adding type checking and intellisense (f.e. when viewing the code in VS Code, etc), without modifying the functionality of the code. Everything will work exactly as it does now.


For a following PR, one thing that I would like to change is to remove the use of global variables for configuration, and instead require options to be passed as attributes or props to the app-javascriptmusic element (or other related APIs). This will make it easy to embed the app anywhere in any site, without polluting global state; i.e. makes it more self-contained.

For yet another PR, I'd like to work on making the code more consumable (f.e. set it up to be publishable on NPM and easy to import and use in any project). In particular, I'd like to decouple the existing UI from the audio engine (JS + AS stuff) to make it easy to import and use the JS/AS APIs standalone without pulling in the code editor UI, without the visualization, etc. Decoupling the code by removing global variables and enforcing inputs and outputs between the components will help with this.

trusktr commented 4 years ago

I am running into a problem with VS Code and the language service not updating the error state of the intellisense. https://github.com/microsoft/TypeScript/issues/37955

It's probably due to a combination of no .ts files, there's no locally installed typescript, there's not node_modules and it is using the typescript that is built into VS Code from a different path, etc.

I have a feeling the issue will probably go away after adding some more options and making the setup more like standard TypeScript projects.

When you checkout this branch, and open it in VS Code, you should see errors in many places (because many types are missing, and the checkJs option in tsconfig.json forces TypeScript/VSCode to complain about type errors in plain JS when otherwise errors in JS would be skipped).

Then, when you start editing any files, you should notice the red squigglies do not update. Hopefully someone other than me can reproduce the issue.

In the meantime, I'll update the project to be more like a normal TS project (but only with type checking, no build), and see if the issue goes away.

trusktr commented 4 years ago

Regarding moving away from global vars, we could start by storing everything on the app-javascriptmusic element instance. This doesn't decouple things, but at least it would make everything self-contained to the element (the easiest way with least refactoring) by using the element instance as a namespace so to speak.

As yet another PR idea for later, it would be nice to organize the repo so that top-level files are meta files related to repo and code management, then the code in few top-level folders. As an example, my repo at https://github.com/lume/glas has meta files at the top, and everything else in the src folder.

petersalomonsen commented 4 years ago

Thanks @trusktr ! This looks really useful. I like the idea of moving away from global vars too, good to make it self-contained.

trusktr commented 4 years ago

Yaaay, they fixed the bug I mentioned. https://github.com/microsoft/TypeScript/issues/37955

I'll get back to this soon. That bug was making types in plain JS very unusable.

trusktr commented 4 years ago

TypeScript 3.9 is out. Looks like I can swing back around to this pull request soon, without that aforementioned issue being in the way!

petersalomonsen commented 4 years ago

Great :-) I've added some tests since last time (added mocha and karma). As the project is growing it's good to have some tests to prevent things from breaking.

trusktr commented 4 years ago

Nice!! Hey Peter, are you on Discord yet? AssemblyScript moved to Discord: https://discord.gg/YMW3wXT

I'd like to circle back to this soon, and have some questions. Would be great to chat (easier).

petersalomonsen commented 4 years ago

Yeah, I’m already in there. My handle is psalomo