# How prevent type inference?

**URL:** <https://discuss.kotlinlang.org/t/how-prevent-type-inference/8140>\
**Category:** Language Design\
**Created:** [June 13, 2018, 12:56pm UTC](https://discuss.kotlinlang.org/t/how-prevent-type-inference/8140 "2018-06-13T12:56:58Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![zcb7kbgxtgjt](https://avatars.discourse-cdn.com/v4/letter/z/e47774/32.png) [@zcb7kbgxtgjt](https://discuss.kotlinlang.org/u/zcb7kbgxtgjt)\
**Post date:** [June 13, 2018, 3:03pm UTC](https://discuss.kotlinlang.org/t/how-prevent-type-inference/8140/4 "2018-06-13T15:03:49Z")

</div>

I found temporary workaround for those who are ready to possible future refactoring.  
It may be kotlin compiler or ide bug:

1. Create file `kotlin/internal/Annotations.kt` in source root of your project:

```auto
package kotlin.internal

@Target(AnnotationTarget.TYPE)
@Retention(AnnotationRetention.BINARY)
annotation class Exact

```

1. Add compiler command line parameter: -Xallow-kotlin-package
2. Using this annotation:

```auto
import kotlin.internal.Exact

open class Foo(val barInt: Int)

// public interface KProperty1<T, out R> : KProperty<R>, (T) -> R

fun <E : Foo, T> test(field: kotlin.reflect.KProperty1<E, @Exact T>, value: T): T = TODO()

fun main(args: Array<String>) {
    test(Foo::barInt, 123) // ok
    test(Foo::barInt, "NotInt") // expected compile error:
    /*
        Error:(13, 5) Kotlin: Type inference failed: Cannot infer type parameter T in fun <E : Foo, T> test(field: KProperty1<E, T>, value: T): T
        None of the following substitutions
        (KProperty1<Foo, Int>,Int)
        (KProperty1<Foo, String>,String)
        can be applied to
        (KProperty1<Foo, Int>,String)
    */
}

```

---

_[View the full topic](https://discuss.kotlinlang.org/t/how-prevent-type-inference/8140)._
