gohugoio/hugo · error
invalid slice type %T
Error message
invalid slice type %T
What it means
Returned by `commonResource.Slice` when the argument is neither `resource.Resources` nor `[]any`. This is the `default` arm of the type switch at resource.go:299-301, hit when the caller passes a typed slice other than `Resources`, a single resource, a map, or a scalar.
Source
Thrown at resources/resource.go:300
// for the template functions. See collections.Slice.
func (commonResource) Slice(in any) (any, error) {
switch items := in.(type) {
case resource.Resources:
return items, nil
case []any:
groups := make(resource.Resources, len(items))
for i, v := range items {
g, ok := v.(resource.Resource)
if !ok {
return nil, fmt.Errorf("type %T is not a Resource", v)
}
groups[i] = g
{
}
}
return groups, nil
default:
return nil, fmt.Errorf("invalid slice type %T", items)
}
}
type fileInfo interface {
setOpenSource(hugio.OpenReadSeekCloser)
setSourceFilenameIsHash(bool)
setTargetPath(internal.ResourcePaths)
size() int64
hashProvider
}
type hashProvider interface {
hash() uint64
}
var _ resource.StaleInfo = (*StaleValue[any])(nil)
type StaleValue[V any] struct {View on GitHub (pinned to 52c9bd7908)
Solutions
- Coerce the input to `[]any` of Resources, or pass a `resource.Resources` value directly.
- Use `collections.Slice` to normalize before calling resource helpers.
- Verify the template variable holds a list of resources, not a single resource or a Page resource collection.
Example fix
// template -- before
{{ apply .Resources.ByType "image/*" "resourceSlice" }}
// after
{{ $imgs := .Resources.Match "images/*" }} Defensive patterns
Strategy: type-guard
Type guard
func isResourceSlice(v any) bool {
switch v.(type) {
case resource.Resources, []any:
return true
}
return false
} Prevention
- Coerce typed slices to `[]any` via `collections.Slice` before calling resource adapters.
- Avoid passing `page.Resources` to resource-side helpers.
When it happens
Trigger: Calling the resource slice adapter with a `[]string`, `[]Page`, single `Resource` value, or any non-slice. The type switch only accepts `resource.Resources` and `[]any`.
Common situations: Template passes `.Page.Resources` typed as `page.Resources` (not `resource.Resources`) into a resource-collection helper; passing a single Resource where a list is expected; passing a typed Go slice through reflection.
Related errors
- type %T is not a Resource
- type %T is not a WeightedPage
- invalid slice type %T
- %T cannot be merged by language
- called apply using {xt} as type {targ}
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/8352e4c3b934c81c.
Report an issue: GitHub.