[Solved] How can instance an object of type dynami...
# javascript
f
[Solved] How can instance an object of type dynamic without
js()
function? Code:
Copy code
external fun require(module: String): dynamic
fun App() { 
  val Koa        = require("koa")
  val app        = Koa() //Dont work: Generate Koa() in JS script need new Koa() 
  val app        = js("new Koa()") // GOOD
}
Generate this:
Copy code
var Koa = require('koa');
var app = Koa(); //BAD: i need new Koa() in JS script
t
Copy code
@JsModule("koa")
external class Koa

fun App() {
    val app = Koa()
}
👍 1
Yes 🙂
Fixed
f
@turansky, Thx I have tried it and it works:
Copy code
@JsModule("koa")
external class Koa
fun App() {
    val app = Koa()
}
Last question: I need the variable
app
to be dynamic to call js functions. I tried this: Any Alternative?
Copy code
val app:dynamic = Koa()
app.use()
t
Copy code
@JsModule("koa")
external class Koa {
    fun use()
}
f
Is very verbose, but ok.
t
Copy code
val app = Koa().asDynamic()
app.use()
f
better thx
@turansky, You would advise against using
external fun require (module: String): dynamic
by
@JsModule
. Official doc.: Says to use
@JsModule
but it says nothing about
external fun require (module: String)
.
t
require
- low level api (block optimization in most cases)
@JsModule
used for strict typization (loading mode - implementation details) I real live both can be used (I use both), depends on your case
@JsModule
in most cases
🤩 2
FYI -
require
can be configured by webpack loaders