capacitor-community / barcode-scanner

A fast and efficient (QR) barcode scanner for Capacitor
MIT License
436 stars 172 forks source link

Make `ScanResult` a union type instead of an interface #196

Closed TheOnlyTails closed 1 year ago

TheOnlyTails commented 1 year ago

This should greatly improve the type inference when checking the result with result.hasContent. Right now, TypeScript has no idea that if hasContent is true, that implies something about the type of result.content, but this PR gives it that context.

(Do let me know if this is made impossible by iOS/Android platform limitations, I'm just making this since I ran into this in my project.)

thegnuu commented 1 year ago

@TheOnlyTails makes sense to add this in my opinion :) Thank you!

I am not entirely sure about the existence of this hasContent property to be honest, since if a scan happens there will always be some sort of content. It was always there and I don't think that it is really required anyways.

I am working on a new version that will drop this thing entirely and just don't call any callbacks as long no content can be read.

I will still add this to the current version since I am not sure how long it will take me to finish the other stuff :)

TheOnlyTails commented 1 year ago

@thegnuu i messed up, i forgot types require = before the brackets, so this will completely break the definitions.

thegnuu commented 1 year ago

No worries, I will test it anyway and fix it ☺️ Currently working on some other stuff anyways that's why I just merged it without testing ☺️