NativeScript / android

NativeScript for Android using v8
https://docs.nativescript.org/guide/android-marshalling
Apache License 2.0
524 stars 134 forks source link

Annotations with TypeScript decorators #1355

Open jdnichollsc opened 5 years ago

jdnichollsc commented 5 years ago

Hello guys,

I was reviewing the documentation for Android, but I can't see any example using Android annotations with TypeScript, can you give me any example? otherwise it would be a nice to have!

I want to use Annotation Type Subscribe of this Android package: http://greenrobot.org/files/eventbus/javadoc/3.0/ from an extended class of java.lang.Object

Any help is really appreciated, I'm creating my first contribution (plugin) for NativeScript and it works perfect from iOS! 👍

Regards, Juan

jdnichollsc commented 5 years ago

I have the following issue testing decorators:

- error TS2339: Property 'Subscribe' does not exist on type 'typeof eventbus'.

5 const Subscribe = org.greenrobot.eventbus.Subscribe;

And my definition file is:

declare namespace org {
  export namespace greenrobot {
    export namespace eventbus {
      export class EventBus {
        public static getDefault(): EventBus;
        public post(event: java.lang.Object): void;
        public isRegistered(subscriber: java.lang.Object): boolean;
        public register(subscriber: java.lang.Object): void;
        public unregister(subscriber: java.lang.Object): void;
      }

      export interface Subscribe {}
    }
  }
}

Let me know, Thanks in advance

jdnichollsc commented 5 years ago

without success testing with a decorator from the android.d.ts:

export function Subscribe<T>(
  target: Object,
  propertyName: string,
  descriptor: TypedPropertyDescriptor<T>
): PropertyDescriptor;

Terminal: System.err: > Interface implementation must be invoked as a constructor with thenewkeyword.

jdnichollsc commented 5 years ago

@EddyVerbruggen do you have experience using EventBus with NativeScript or RxJS would be a better option?

EddyVerbruggen commented 5 years ago

@jdnichollsc No, but have you tried using https://github.com/NativeScript/android-dts-generator to generate those typings?

jdnichollsc commented 5 years ago

@EddyVerbruggen thanks! but this issue looks like Android annotations are not supported with NativeScript and It would be awesome using it with TypeScript decorators :)

EddyVerbruggen commented 5 years ago

I understand, but can't really help you with that.

jdnichollsc commented 5 years ago

No problem, thanks mate! 👍

vmutafov commented 5 years ago

Hi @jdnichollsc! Sorry for the late reply, if I understand correctly, you want to extend a Java class and decorate it with a Java annotation. Something like that:

@JavaAnnotation
class JSClass extends JavaClass{
}

If this is the case, currently we do not support setting Java annotations through TypeScript extending of Java classes. This perhaps would not be trivial to support but I'll think it through when working on the above mentioned feature request