External declarations
external keyword allows to declare existing JavaScript API in a type-safe way.
external fun alert(msg: String) // 1
fun main() {
alert("Hi!") // 2
}
- Declares an existing JavaScript function
alert
which takes a singleString
argument. - Uses
alert
as if it were regular Kotlin.
Note that Kotlin checks during compilation, that a single argument of type String is passed. Such a check prevents some bugs even when using pure JavaScript API.
Please refer to the docs in order to learn more about describing existing JavaScript API.