Fix typeName panic for interface type parameters

reflect.TypeOf on a nil interface value returns nil, causing a
panic when Kind() is called. Use reflect.TypeFor[T]() instead,
which resolves the type directly from the type parameter.

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2026-03-13 19:34:07 +01:00
parent 0c3893ee8b
commit 1f5e68abe6
2 changed files with 19 additions and 2 deletions

View File

@@ -69,8 +69,7 @@ func RunTyped[T any](
}
func typeName[T any]() string {
var zero T
t := reflect.TypeOf(zero)
t := reflect.TypeFor[T]()
if t.Kind() == reflect.Pointer {
t = t.Elem()
}

View File

@@ -120,6 +120,24 @@ func TestTypeName(t *testing.T) {
assert.Equal(t, "status", typeName[Status]())
},
)
t.Run(
"any interface returns output",
func(t *testing.T) {
t.Parallel()
assert.Equal(t, "output", typeName[any]())
},
)
t.Run(
"named interface returns lowercased name",
func(t *testing.T) {
t.Parallel()
assert.Equal(t, "error", typeName[error]())
},
)
}
func TestRunTyped(t *testing.T) {