JavaScript Reflection

At this time, JavaScript does not support the full Kotlin reflection API. The only supported part of the API is the ::class syntax which allows you to refer to the class of an instance, or the class corresponding to the given type. The value of a ::class expression is a stripped-down KClass implementation that only supports the simpleName and isInstance members.

In addition to that, you can use KClass.js to access the JsClass instance corresponding to the class. The JsClass instance itself is a reference to the constructor function. This can be used to interoperate with JS functions that expect a reference to a constructor.


class A
class B
class C

inline fun <reified T> foo() {

val a = A()
println(a::class.simpleName)  // Obtains class for an instance; prints "A"
println(B::class.simpleName)  // Obtains class for a type; prints "B"
println(B::class.js.name)     // prints "B"
foo<C>()                      // prints "C"

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.