as-json icon indicating copy to clipboard operation
as-json copied to clipboard

Compile Time check for json decorator

Open anyon17 opened this issue 1 year ago • 5 comments

Right now a class decorated with @json containing fields which might not have types whose declaration have @json decorator panics at the runtime. Is there any way to have this check at compile time so that compiler can exactly point to the field which does not have @json decorator in it's declaration.

anyon17 avatar Oct 14 '24 15:10 anyon17

Here's an example for context A class decorated with @json, but not all fields implement the JSON Type

@json
class Foo {
  public bar: string = "this is ok"
  public baz: i32 | null = 0
  // ^ this is not okay
}
Threw "unreachable" at runtime!
 at 0:30 ...

This will fail miserably and throw something like "index out of range" or "unreachable" which are highly non-descriptive. Rather:

@json
class Foo {
  public bar: string = "this is ok"
  public baz: i32 | null = 0
  // ^ this is not okay
}

ERROR: Property Foo.baz in <./src/index.ts> does not implement a JSON-compatible type!
Either decorate it with the `@omit` decorator or fix it!

Instead, we actually throw at COMPILE TIME to ensure that the user has fault tolerant code

JairusSW avatar Oct 14 '24 15:10 JairusSW

Yeah that's perfectly put @JairusSW . Thanks for attaching the example to make the issue much clear.

anyon17 avatar Oct 14 '24 16:10 anyon17

Reopening this as there are a few bugs

JairusSW avatar Oct 21 '24 23:10 JairusSW

@anyon17, after taking a closer look, it seems like to properly implement this (and a few other things), I'm going to have to take multiple passes over the AST via the transform. I'm not sure if your familiar with transforms, but its a pretty big undertaking, so I'll pull some features out and release a non-feature-complete version that's stable. This means I'll add full type checking in a later release. I'm still going to be working on this, just thought I'd let you know it may take a bit longer.

JairusSW avatar Oct 22 '24 00:10 JairusSW

hey @JairusSW thanks for letting me know this detail. Will wait for the feature to become stable and looking forward to use it. Thanks again for picking up the requested feature.

anyon17 avatar Oct 24 '24 16:10 anyon17