Using JS function returning dynamic in Kotlin JS within the default Promise constructor results in ClassCastException

I got this error when trying to transform gapi calls into Promises, but I suppose calls to other functions may result in the same issue. First, I added the library as a script in the page:

script(src = "https://apis.google.com/js/platform.js") {
    async = true
    defer = true
}

next, I fetched gapi object from window:

import kotlinx.browser.window
import org.w3c.dom.get

val gapi = window["gapi"]

finally, I used the load function like so:

import kotlin.js.Promise

fun loadGapi() = Promise<Unit> { resolve, _ ->
    gapi.load("client:auth2") { resolve(Unit) }
}

giving me a warning in the IDE:

IDE warning: Implicit (unsafe) cast from dynamic to Unit

This resulted in the following error when called inside a page:

Uncaught (in promise) ClassCastException {message: undefined, cause: undefined, name: ‘ClassCastException’, stack: ‘ClassCastExceptionn at THROW_CCE (http://localh… (http://localhost:3000/static/app.js:4930:14)’}