-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to declare objects with methods in TypeScript spec file? #27
Comments
Hi @asfernandes. I don't think that you should do that. Those object are meant to be data object, which only contains data. If you need to provide behaviors, then you should implement that in your Native Module. To better understand what you are trying to achieve, why the object should have behaviour attached? What's the use case? |
Why these objects are meant to be data objects? I solved my problem declaring a function in the spec that returns In native code this is a Then I had to force a cast from Object to some interface declared in another TypeScript file. I cannot understand why it can't be declared correctly in the TypeScript' spec file. |
You can declare it correctly in the TypeSpec file. You may have object with behavior, in TypeScript. However, the spec files are used to generate objects in the Native platform code, and we can't generate the behavior. Your approach works, but you have to give up type safety: in fact you had to cast your object to what you needed. That's is doable but a bit risky, as casting is usually not advisable. Does it make sense? What could be done is to have only the interface declared and codegen'd in platform code, but then we would have to create a mechanism to implement the behavior without the risk that codegen override those files. |
For example, in https://github.com/react-native-community/RNNewArchitectureApp/blob/run/pure-cxx-module/tm/NativeSampleModule.ts if
CustomType
has methods, how to declare them?The text was updated successfully, but these errors were encountered: