{"id":"3430df68f9e56671","repo":"gohugoio/hugo","slug":"unable-to-convert-value-of-type-q-to-q","errorCode":null,"errorMessage":"unable to convert value of type %q to %q","messagePattern":"unable to convert value of type %q to %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/reflect_helpers.go","lineNumber":100,"sourceCode":"func convertValue(v reflect.Value, to reflect.Type) (reflect.Value, error) {\n\tif v.Type().AssignableTo(to) {\n\t\treturn v, nil\n\t}\n\tswitch kind := to.Kind(); {\n\tcase kind == reflect.String:\n\t\treturn hreflect.ToStringValueE(v)\n\tcase hreflect.IsNumber(kind):\n\t\treturn convertNumber(v, to)\n\tdefault:\n\t\treturn reflect.Value{}, fmt.Errorf(\"%s is not assignable to %s\", v.Type(), to)\n\t}\n}\n\nfunc convertNumber(v reflect.Value, typ reflect.Type) (reflect.Value, error) {\n\tif v, ok := hreflect.ConvertIfPossible(v, typ); ok {\n\t\treturn v, nil\n\t}\n\treturn reflect.Value{}, fmt.Errorf(\"unable to convert value of type %q to %q\", v.Type().String(), typ.String())\n}\n\nfunc newSliceElement(items any) any {\n\ttp := reflect.TypeOf(items)\n\tif tp == nil {\n\t\treturn nil\n\t}\n\tswitch tp.Kind() {\n\tcase reflect.Array, reflect.Slice:\n\t\ttp = tp.Elem()\n\t\tif tp.Kind() == reflect.Pointer {\n\t\t\ttp = tp.Elem()\n\t\t}\n\n\t\treturn reflect.New(tp).Interface()\n\t}\n\treturn nil\n}","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/collections/reflect_helpers.go#L82-L118","documentation":"When Hugo's collection functions (append, union, intersect, sort comparisons, etc.) mix element types, `convertValue`/`convertNumber` try to coerce a value to the target slice's element type via reflection. If the numeric conversion is not possible (e.g. the value would not fit or the kinds are incompatible per `hreflect.ConvertIfPossible`), Hugo reports the source and destination types in this error.","triggerScenarios":"Appending or set-combining values into a typed slice whose element type the value cannot convert to — e.g. appending a negative int or a float into a `[]uint`, or mixing incompatible numeric types where lossless conversion fails.","commonSituations":"Mixing data-file values (JSON numbers decode as float64) with typed slices built in templates or from Go-side params; appending template-computed ints into slices originating from config with a narrower/unsigned type; version changes that tightened type handling in collections functions.","solutions":["Normalize types before combining: cast values explicitly with `int`, `float`, or `string` so both operands share a type, e.g. `{{ $s = $s | append (int $v) }}`.","Build the slice from scratch with `slice` using consistently typed values instead of appending mixed types into an existing typed slice.","If the data comes from JSON/YAML, remember numbers may be float64 — cast at the point of use."],"exampleFix":"<!-- before -->\n{{ $nums := site.Params.counts }}{{ $nums = $nums | append \"7\" }}\n<!-- after -->\n{{ $nums := site.Params.counts }}{{ $nums = $nums | append (int \"7\") }}","handlingStrategy":"type-guard","validationCode":"{{ $a := slice 1 2 }}\n{{ $b := slice \"3\" \"4\" }}\n{{ if eq (printf \"%T\" (index $a 0)) (printf \"%T\" (index $b 0)) }}{{ union $a $b }}{{ end }}","typeGuard":"{{ $sameType := eq (printf \"%T\" $x) (printf \"%T\" $y) }}","tryCatchPattern":"{{ with try (intersect $a $b) }}{{ with .Err }}{{ warnf \"type mismatch in collection op: %s\" . }}{{ else }}{{ .Value }}{{ end }}{{ end }}","preventionTips":["Keep both collections element-type-homogeneous before intersect/union/etc. — mixed int/string slices fail conversion","Normalize types explicitly (e.g. `apply` with `string` or `int`) before combining collections from different sources","Front matter can yield surprising types (int vs float vs string); cast params before set operations"],"tags":["hugo","collections","type-conversion","templates"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}