gohugoio/hugo · error

invalid slice type %T

Error message

invalid slice type %T

What it means

Returned by `WeightedPage.Slice` when the argument is neither `WeightedPages` nor `[]any` -- i.e. the top-level type is unrecognized. This is the `default` arm of the type switch at weighted.go:82-84. It is an internal adapter used by `collections.Slice`; reaching it means the caller passed a struct, pointer, map, or scalar where a slice was required.

Source

Thrown at resources/page/weighted.go:83

// Slice is for internal use.
// for the template functions. See collections.Slice.
func (p WeightedPage) Slice(in any) (any, error) {
	switch items := in.(type) {
	case WeightedPages:
		return items, nil
	case []any:
		weighted := make(WeightedPages, len(items))
		for i, v := range items {
			g, ok := v.(WeightedPage)
			if !ok {
				return nil, fmt.Errorf("type %T is not a WeightedPage", v)
			}
			weighted[i] = g
		}
		return weighted, nil
	default:
		return nil, fmt.Errorf("invalid slice type %T", items)
	}
}

// Pages returns the Pages in this weighted page set.
func (wp WeightedPages) Pages() Pages {
	pages := make(Pages, len(wp))
	for i := range wp {
		pages[i] = wp[i].Page
	}
	return pages
}

// Next returns the next Page relative to the given Page in
// this weighted page set.
func (wp WeightedPages) Next(cur Page) Page {
	for x, c := range wp {
		if c.Page.Eq(cur) {
			if x == 0 {

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Wrap the argument so it is a `[]any` of WeightedPage values, or a `WeightedPages` value.
  2. Use `collections.Slice` which normalizes inputs to `[]any` before dispatching.
  3. Verify the template variable is a taxonomy/collection, not a single page, before calling.

Example fix

// template -- before
{{ apply .Page "weightedSlice" }}

// after
{{ apply .Site.Taxonomies.tags "slice" }}
Defensive patterns

Strategy: type-guard

Type guard

func isWeightedPages(v any) bool {
	switch v.(type) {
	case page.WeightedPages, []any:
		return true
	}
	return false
}

Prevention

When it happens

Trigger: A template or internal call passes a non-slice (a single Page, a string, a map, a `Pages` typed slice) into the WeightedPage Slice adapter. Because Go's type switch only matches `WeightedPages` and `[]any`, any concrete slice type like `[]Page` also falls through.

Common situations: Passing `site.GetPage ...` result (a single Page) where a list was expected; passing `Pages` (distinct from `WeightedPages`) into the weighted adapter; template helpers that return `interface{}` holding a typed slice.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/45f4f2648bbe5d38. Report an issue: GitHub.