Cannot use type assertion on type parameter value

tl;dr

You can perform a type assertion only on interface values. So you must convert x to a valid interface type first, any / interface{} in this case:

func isInt[T any](x T) (ok bool) {

    _, ok = any(x).(int) // convert, then assert
    return
}

So why does this fail to compile?

_, ok = x.(int)   // ... cannot use type assertion on type parameter value ...

x‘s type T is a type parameter, not an interface. It is only constrained by an interface. The Go (revised 1.18) language spec explicitly states type parameters are not allowed in a type assertion:

For an expression x of interface type, but not a type
parameter
, and a type T … the notation x.(T) is called a type assertion.


Also from from the generics tutorial on why parameter types need to be resolved at compile-time:

While a type parameter’s constraint typically represents a set of
types, at compile time the type parameter stands for a single type
the type provided as a type argument by the calling code. If the type
argument’s type isn’t allowed by the type parameter’s constraint, the
code won’t compile.

Leave a Comment