You cannot use a type parameter directly in a type assertion because the compiler cannot verify the type at compile time. Instead, assert to any first, then perform a second assertion to the specific type parameter.
func Process[T any](v any) {
// Step 1: Assert to any (or the interface T implements)
if raw, ok := v.(any); ok {
// Step 2: Assert to the specific type parameter
if t, ok := raw.(T); ok {
// Use t
}
}
}
If you know the value is already of type T, simply use a type conversion instead of an assertion:
func Process[T any](v any) {
t := v.(T) // Panics if v is not T
// Use t
}