json2typescript icon indicating copy to clipboard operation
json2typescript copied to clipboard

Support for stage 3 Typescript decorators

Open coddo opened this issue 11 months ago • 3 comments

Typescript >= v5 now has proper decorators implemented, known as stage 3: https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#decorators.

This means that it is no longer necessary to do this in the tsconfig.json file:

{
  "compilerOptions": {
[
  ...
]
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
[...]
}

However, in stage 3, the entire schema has changed on what is sent into the decorators, so it breaks the functionality of the library. For example, in the returned function for the decorator factory on JsonProperty, instead of return function (target: any, classPropertyName: string): void the code to accept the decorator invocation is now return function <C, V>(target: undefined, context: ClassFieldDecoratorContext<C, V>): void

In the above mentioned change, target is now always undefined and will never be sent in to represent the class itself, therefore we need to play with the context metadata to send in the mapping data needed by the JsonObject decorator instead of setting the prototype on the target.

I have already figured out what needs to be changed and have prepared some code changes on my side, but I'm not sure how this impacts all major frameworks when you change it.

The major problem with this is that for all projects using Babel (and other transpilers) this kind of change will not work. All those transpilers will change all stage 3 decorators into stage 2 decorators behind the scenes and I haven't found anything on how to configure this to not happen. So in this case, will it be better to support this by releasing the changes as a new major version from a separate branch just for that? Not sure...

I will fork the project and try to create a PR for you to see the changes, but I'm not sure what's the best way to go around this breaking change in TS.

coddo avatar Jan 22 '25 08:01 coddo

Thank you for your issue.

We definitely need to address this issue.

Many developers use this library through Angular which also heavily relies on experimental decorators. Hence, we need to consider the development at Angular as well.

Is there already a roadmap on the TS side? Or Angular?

If there are breaking changes, we would need to push our library to version 2 and make sure it does not cause problems for existing users.

andreas-aeschlimann avatar Jan 23 '25 14:01 andreas-aeschlimann

I've did some changes locally and manually tested them and they seem to work, but it requires a lot of modifications in tsconfig for targets, moduleResolution etc, so I'm not sure if a v2 is doable without breaking all the major frameworks.

I haven't made the PR yet to show the changes as I can no longer get Karma to function for the automated tests that you have, which shows just how incompatible most dependencies are with the latest versions of Typescript.

Chrome Headless 132.0.0.0 (Linux x86_64) ERROR
  An error was thrown in afterAll
  Uncaught ReferenceError: exports is not defined
  ReferenceError: exports is not defined
      at test/json2typescript.integration.js:2:23
  Uncaught ReferenceError: exports is not defined
  ReferenceError: exports is not defined
      at test/json2typescript.nullable.js:40:23
  Uncaught ReferenceError: exports is not defined
  ReferenceError: exports is not defined
      at test/json2typescript.primitive.js:40:23
  Uncaught ReferenceError: exports is not defined
  ReferenceError: exports is not defined
      at test/json2typescript.unit.js:2:23
  Uncaught ReferenceError: exports is not defined
  ReferenceError: exports is not defined
      at test/model/json/i-animal.js:2:23
  Uncaught ReferenceError: exports is not defined
  ReferenceError: exports is not defined

Should I make a draft PR so you can see the changes that I've made?

I don't really understand everything about bundling for browser compatibility and didn't keep up with all the changes on this with Node, so I'm not entirely sure if what I did is correct in the configuration files. The changes in the 2 decorators JsonObject and JsonProperty however I'm very sure that they are correct as work as intended with stage 3.

coddo avatar Jan 23 '25 14:01 coddo

Unfortunately, I do not know more. Maybe we should keep this issue on hold until a beta version is on the horizon? We are probably way too early.

andreas-aeschlimann avatar Jan 23 '25 15:01 andreas-aeschlimann