JohnWeisz / TypedJSON

Typed JSON parsing and serializing for TypeScript that preserves type information.
MIT License
603 stars 64 forks source link

Allow lazily define type in annotations / ES modules #189

Open perceptron8 opened 2 years ago

perceptron8 commented 2 years ago

Because of somewhat eager https://github.com/JohnWeisz/TypedJSON/blob/6f886ae669c351c6dfc3d02e853bc6294cf3d054/src/json-member.ts#L195 it's currently impossible to define types truly lazily. Above call causes serious problems when ES modules are being used.

To reproduce, you can review and

git clone https://github.com/perceptron8/TypedJSONReferenceError
cd TypedJSONReferenceError
yarn install
yarn test

-> ReferenceError: Cannot access 'A' before initialization.

"emitDecoratorMetadata" (tsconfig.json) is deliberately set to false, so it's not because of https://github.com/microsoft/TypeScript/issues/4521.

If you change "type" to "commonjs" (package.json) and "compilerOptions" / "module" to "CommonJS" (tsconfig.json), test will run fine. BTW that's exactly why your lazy-types.spec.ts isn't failing: https://github.com/JohnWeisz/TypedJSON/blob/6f886ae669c351c6dfc3d02e853bc6294cf3d054/tsconfig/tsconfig.spec.json#L4

I know that typeToTest is currently being used to check for special property types, but maybe this checking could be deferred or - in case of jsonMemberDecoratorFactory - skipped at all?


This issue refers to: #139.

AnkoGo123 commented 1 year ago

I had the same problem. It was a real headache.

MatthiasKunnen commented 1 year ago

Hi, I'm the author of the PR that introduced support for circular dependencies to this library. I had hoped to become a maintainer and improve this library further but unfortunately, the owner is not receptive to breaking changes and very slow to respond. This lead to my implementation being amended with imperfect backwards compatibility.

Since then, I have forked the library to https://github.com/decoverto/decoverto and reworked the code; simplified options, increased test coverage to 100%, better lazy types implementation, better inheritance support, and much more.

Modifying your reproduction to use Decoverto succeeds for me @perceptron8. See https://github.com/MatthiasKunnen/TypedJSONReferenceError/commit/f8703fdc9e70cf6c8dbcc2f8644da7fafdd3a86b.

Perhaps using Decoverto can help you.

perceptron8 commented 1 year ago

@MatthiasKunnen Thanks for letting me know! :) https://github.com/JohnWeisz/TypedJSON doesn't seem to be very much alive.

Because of deadlines, I had to write TS<->JSON parsing / serializing library from scratch. Unfortunately I'm not allowed to share it as I wasn't writing it in my private time (this lib is now property of a company I work in).

I hope that https://github.com/decoverto/decoverto relieves at least @AnkoGo123's pain ;)

Thanks again and good luck!

MatthiasKunnen commented 1 year ago

@perceptron8 Thank you!

It's a shame I missed this issue a year ago, perhaps I could've saved you the effort of creating your own library. If at any time in the future you have a need for another serialization library perhaps we'll meet again :)