Dynamic Type
Kotlin is a statically typed language, which makes it different from the dynamically typed JavaScript. In order to facilitate interoperation with JavaScript code, Kotlin/JS offers the type:
The dynamic
type basically turns off Kotlin’s type checker:
- A
dynamic
value can be assigned to variables of any type, or passed anywhere as a parameter. - A
dynamic
variable can have a value of any type. - A function that takes a
dynamic
parameter can take arguments of any type.
This code will be compiled “as is”: dyn.whatever(1)
in Kotlin becomes dyn.whatever(1)
in the generated JavaScript code.
When calling functions written in Kotlin on values of dynamic
type, keep in mind the name mangling performed by the Kotlin to JavaScript compiler. You may need to use the @JsName annotation or the to assign well-defined names to the functions that you want to call.
When we pass a lambda to a dynamic call, all of its parameters by default have the type dynamic
:
Expressions using values of dynamic
type are translated to JavaScript “as is”, and do not use the Kotlin operator conventions. The following operators are supported:
- binary:
+
, ,*
,/
,%
,>
,<
>=
,<=
,==
,!=
,===
,!==
,&&
,||
- unary
- prefix:
-
, ,!
- prefix:
- assignments:
+=
,-=
,*=
,/=
,%=
- indexed access:
- read:
d[a]
, more than one argument is an error
- read:
For a more technical description, see the spec document.