2

I am trying out Kotlin/JS.

As an exercice to learn something new I wanted to mess with WebRTC. I've been following the basic examples on their site and at some point you have to instantiate an object from the RTCPeerConnection class. Something very straight forward and available without installing any dependencies.

If I come on the browser's console I can easily do:

Browser screenshot

However in my IntelliJ the compiler screams at me stating that there is no such class so I can't instantiate it.

Now I know of two ways I could get around to this:

  1. Using fun js(code: String): dynamic, which works but I lose all intelli-sense and type-safety making me really sad (and kind of missing the point of using Kotlin)
  2. Declaring (using external keyword) the entirety of the RTC spec which is very extense and that also makes me really sad

I've found out a way to hack my way around the second approach but still is not good enough:

I've installed @types/webrtc from NPM in this fashion:

implementation(npm("@types/webrtc", "0.0.30", generateExternals=true))

So I could trick Dukat to generate the external declarations for me and I've actually succeeded!

… or that's what I thought

Error

Yeah, seems like they've declared the RTCPeerConnection as an interface and not as a class so I am out of luck

Has somebody worked this out of it?

Some random IT boy
  • 7,569
  • 2
  • 21
  • 47
  • can you use the `js()` blob and then cast to the generated dukat type ? – Nikky Oct 25 '21 at 00:13
  • That's a smart approach, I'll give it a shot – Some random IT boy Oct 25 '21 at 07:10
  • No, it doesn't work. Because when attempting to compile the Kotlin/JS project seems like Dukat generates repeated properties and it does not compile – Some random IT boy Oct 26 '21 at 13:12
  • As workaround you can generate declarations from "@types/webrtc" into kt files using standalone version of dukat or through gradle integration with `generateExternals` task, for more information check out https://kotlinlang.org/docs/js-external-declarations-with-dukat.html – bashor Jan 21 '22 at 13:36
  • This was suggested on reddit. It did not work because the types generated by Dukat had duplicated properties and the code wouldn't compile at all! – Some random IT boy Jan 21 '22 at 22:37
  • in that case you sadly have to disable dukat for that dependency and write the external declarations yourself.. ideally using the not quite working dukat stuff as a base – Nikky Mar 20 '22 at 17:25

0 Answers0