NativeScript / NativeScript/NativeScript
Include all transparently marshaled classes in type definitions
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 25.7k
- Forks
- 1.7k
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 35
Description
Is your feature request related to a problem? Please describe.
This doesn't cause any problems except that it's not always clear what types can be passed to a native function. I ran into this when looking at https://github.com/NativeScript/ios-runtime/pull/235/, and am not sure how many other types this would apply to.
Describe the solution you'd like
It would be nice if the type definitions for all native functions which accept JS or native classes in their parameters would reflect that as a union type. For instance the following TypeScript both compiles and runs as expected:
const f = (buffer:ArrayBuffer):NSData => {
return(NSData.dataWithData(buffer as any));
}
However, the any cast is circumventing the type system, so it's not clear whether this would actually work when running on the device. I would rather write it as follows, but this does not currently compile:
const f = (buffer:ArrayBuffer):NSData => {
return(NSData.dataWithData(buffer));
}
I assume the type definitions are generated automatically by some script, but my naive approach would be doing something like this:
declare type NSDataLike = NSData|ArrayBuffer;
declare class NSData extends NSObject implements CKRecordValue, NSCopying, NSMutableCopying, NSSecureCoding {
static dataWithData(data: NSDataLike): NSData;
Describe alternatives you've considered
I realize I could re-declare any native class in my project's code, but it seems like this would need to be done piecemeal, and in any case this would not help the declarations to act as documentation for other users. I'm not familiar enough with the internals of the TypeScript type system to suggest more sophisticated workarounds.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Begin with the generated TypeScript declarations for NSData and its dataWithData method, then trace the definition-generation path for native functions that accept transparently marshaled classes. Done means the declarations expose the applicable union types so the ArrayBuffer example compiles without an any cast.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, mobile
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100