ErnstHot / TypeScript-for-Max

TypeScript for Cycling '74 Max
Other
90 stars 12 forks source link

[question] readme#using-the-definitions-in-your-own-project #29

Closed FrancescElies closed 1 year ago

FrancescElies commented 1 year ago

Here I read the following.

Max uses an archaic es3 javascript, which means that for lots of modern functionality you will need to include polyfills. In our tsconfig.json we define this "target": "es3", and this means that we also need to set "ignoreDeprecations": "5.0", because es3 support will soon be dropped by Typescript

tsconfig.json includes "lib": ["es6"] to stop Typescript from trying to use DOM declarations. Not including this lib directive results in duplicate declaration errors because some max items share names with DOM items.

Both points make sense to me, but when I read tsconfig#lib docs shouldn't lib be es5 instead of es6?

What am I missing?

twhiston commented 1 year ago

This is an interesting one. Cycling '74 don't make it clear exactly what engine they are running in the [js] object, and they do have a number of es6 functions implemented from what I can tell, although not all! It's a bit of polyfill roulette in my experience, so I'm not actually sure if es5 or es6 is better. I've made a number of fairly complex projects with lib set to es6 though and not had any huge issues. You would be very welcome to do the research to conclusively prove which would be best though if you're prepared to spend the time on it! Hopefully in future we might get a version of max with a newer js engine, but that's complete conjecture at this point as they've been pretty tight lipped on what plans they have.

FrancescElies commented 1 year ago

The only thing I can tell is what I read here. The following answer coming from what I believe is a dev @Cycling74 (not 100% sure about that).

image

Maybe we could ask in this typescript thread?

twhiston commented 1 year ago

I've seen that post before but I have to say that if I targeted ES5 with Typescript I had problems, which is why I "target": "ES3" now in all my projects. However changing to "lib": ["ES5"] may well make sense if we can validate that nothing is broken by doing so. It should not be though as essentially we are just using this to make sure the DOM declarations are not included. Interestingly enough I also discovered that you may need to actually do

"lib": [
      "ES6",
      "es2017.object"
    ]

depending on what you include in your project. See https://github.com/twhiston/tw.gl.repl for an example of this.

twhiston commented 1 year ago

I tried to rebuild my latest feature branch of the above project with "lib": ["ES5"] instead of what I said above and at first glance it seems like it works. I'll try to give it a complete run through of all the functionality later as that's probably the most complex bit of max typescript I have, but I'm coming around to the idea that ES5 might be the right call here.

twhiston commented 1 year ago

I ran a load of tests on this and it seems lib and target set to es5 seems to work in every case, so I have updated the examples and the README. Thanks very much for pointing this out and compelling me to do some more digging into it