Unresolved reference: typeOf


I’m trying to get a KType for a generic map, and using the following example from the docs gives “Unresolved reference: typeOf”. This is with 1.6.10 so typeOf is stable. Is there something I need to enable to get typeOf working?

inline fun <reified T> renderType(): String {
    val type = typeOf<T>()
    return type.toString()

fun main() {
    val fromExplicitType = typeOf<Int>()
    val fromReifiedType = renderType<List<Int>>()

Have you imported this function from kotlin.reflect?

Note that in IDEA 2021.3+ there is a bug that the auto-import doesn’t work for typeOf, so you will have to write the import declaration manually: https://youtrack.jetbrains.com/issue/KTIJ-20328

1 Like

Ah, that’s probably the case. I was expecting to see auto-import and only saw the other options, so assumed it was already imported somehow. Thanks