Hookyns / tst-reflect

Advanced TypeScript runtime reflection system
MIT License
338 stars 11 forks source link

Getters and setters ? #8

Closed caiodallecio closed 2 years ago

caiodallecio commented 2 years ago

Is it possible to check if a given property has/is a getter or setter? Would you be interested in a PR ?

Hookyns commented 2 years ago

Hi @caiodallecio,

it is not possible now, but it is possible, PR is welcome. Getters & setters are returned with fields by Type.getProperties().

We can extend interface Property by isSetter() and isGetter() and join that with #4 and add isPrivate(), isProtected(), isPublic() and isReadOnly().

This means:

  1. Add enum Accessor { Getter, Setter } to the runtime package (runtime/reflect.ts),
  2. same as enum AccessModifier { Private, Protected, Public }.
  3. Extend interface PropertyDescription by properties acs: Accessor, am: AccessModifier, ro: boolean (is readonly) https://github.com/Hookyns/ts-reflection/blob/1a93314179f739e91fb183dcb72cb95e13ca4aa7/runtime/reflect.ts#L120-L125 XxxDescription interfaces are for metadata generated by the transformer, so names are shortcuts to keep metadata small.
  4. Extend interface Property by methods:
    1. isGetter(): boolean,
    2. isSetter(): boolean,
    3. isPrivate(): boolean,
    4. isProtected(): boolean,
    5. isPublic(): boolean,
    6. isReadOnly(): boolean https://github.com/Hookyns/ts-reflection/blob/1a93314179f739e91fb183dcb72cb95e13ca4aa7/runtime/reflect.ts#L130-L146
  5. Implement these new features in runtime class Type,
  6. and finally implement this in transformer (transformer/src/getProperties.ts)

PS: getter & setter example https://replit.com/@Hookyns/tst-reflect-example-getters-setters

Hookyns commented 2 years ago

Information about getters and setters is available from tst-reflect@0.6.0-alpha.5 and tst-reflect-transformer@0.8.5-alpha.0

PS: used in ./dev/class-info example