gohugoio/hugo · error
called apply using {xt} as type {targ}
Error message
called apply using {xt} as type {targ} What it means
In applyFnToThis (apply.go:101-103) each argument's reflect type is tested with AssignableTo against the target function's parameter type. If an arg (including the "." current item) is not assignable, it surfaces both types. This is the type-mismatch gate for reflective invocation.
Source
Thrown at tpl/collections/apply.go:102
if fn.Type().IsVariadic() {
num--
}
// TODO(bep) see #1098 - also see template_tests.go
/*if len(args) < num {
return reflect.ValueOf(nil), errors.New("Too few arguments")
} else if len(args) > num {
return reflect.ValueOf(nil), errors.New("Too many arguments")
}*/
for i := range num {
// Go's built-in template funcs (e.g. len) use reflect.Value as argument type.
if fn.Type().In(i) == typeOfReflectValue && n[i].Type() != typeOfReflectValue {
n[i] = reflect.ValueOf(n[i])
}
if xt, targ := n[i].Type(), fn.Type().In(i); !xt.AssignableTo(targ) {
return reflect.ValueOf(nil), errors.New("called apply using " + xt.String() + " as type " + targ.String())
}
}
res := fn.Call(n)
if len(res) == 1 || res[1].IsNil() {
return res[0], nil
}
return reflect.ValueOf(nil), res[1].Interface().(error)
}
func (ns *Namespace) lookupFunc(ctx context.Context, fname string) (reflect.Value, bool) {
namespace, methodName, ok := strings.Cut(fname, ".")
if !ok {
return ns.deps.GetTemplateStore().GetFunc(fname)
}
// NamespaceView on GitHub (pinned to 52c9bd7908)
Solutions
- Match the function's parameter type to the slice element type
- Convert elements first (e.g. apply "string") before the target function
- Use a partial that accepts the actual element type
Example fix
// before
{{ apply . "absURL" }} {{/* . is []int */}}
// after
{{ apply (apply . "string") "absURL" }} Defensive patterns
Strategy: type-guard
Validate before calling
{{/* convert numeric elements to string before a string function */}}
{{ $strs := apply . "string" }}
{{ apply $strs "upper" }} Type guard
{{/* Go-side guard for programmatic callers */}}
func isStringSlice(v any) bool {
t := reflect.TypeOf(v)
return (t.Kind() == reflect.Slice || t.Kind() == reflect.Array) && t.Elem().Kind() == reflect.String
} Prevention
- Match the function's parameter type to the slice element type
- Pre-convert with apply "string" when targeting string funcs
- Keep element types stable; review after refactors that change element types
When it happens
Trigger: Applying a string-only function (e.g. upper) to a slice of ints; passing an int arg where the func expects a string; the current element type disagrees with the func's first parameter.
Common situations: Applying absURL/upper (string funcs) to a numeric slice; passing a Page where a string is expected; element type changed after a refactor.
Related errors
- intersect does not support slices or arrays of uncomparable
- can't iterate over a nil value
- can't iterate over a nil value
- can't iterate over {type}
- invalid dictionary key
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/09132c18117506e2.
Report an issue: GitHub.