{"record":{"id":"9a7654913c9d6220","repo":"gohugoio/hugo","slug":"failed-to-convert-t-to-a-string-slice","errorCode":null,"errorMessage":"failed to convert %T to a string slice","messagePattern":"failed to convert %T to a string slice","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/types/convert.go","lineNumber":81,"sourceCode":"\t\treturn result, nil\n\t}\n\n\t// Probably []int or similar. Fall back to reflect.\n\tvv := reflect.ValueOf(v)\n\n\tswitch vv.Kind() {\n\tcase reflect.Slice, reflect.Array:\n\t\tresult = make([]string, vv.Len())\n\t\tfor i := range vv.Len() {\n\t\t\ts, err := cast.ToStringE(vv.Index(i).Interface())\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\t\tresult[i] = s\n\t\t}\n\t\treturn result, nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"failed to convert %T to a string slice\", v)\n\t}\n}\n\n// TypeToString converts v to a string if it's a valid string type.\n// Note that this will not try to convert numeric values etc.,\n// use ToString for that.\nfunc TypeToString(v any) (string, bool) {\n\tswitch s := v.(type) {\n\tcase string:\n\t\treturn s, true\n\tcase template.HTML:\n\t\treturn string(s), true\n\tcase template.CSS:\n\t\treturn string(s), true\n\tcase template.HTMLAttr:\n\t\treturn string(s), true\n\tcase template.JS:\n\t\treturn string(s), true","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/common/types/convert.go#L63-L99","documentation":"Returned by types.ToStringSlicePreserveStringE when cast.ToStringSliceE fails and the reflect fallback also fails (value is not a string, slice, or array). The fallback iterates slice/array elements but any other Kind lands in the default branch and yields this error naming the Go type.","triggerScenarios":"Calling ToStringSlicePreserveStringE with a struct, map, number, bool, or pointer to non-slice. nil returns nil,nil; strings are wrapped; slices/arrays are element-wise stringified.","commonSituations":"Config fields expected to be lists but supplied as scalars (e.g. `menu.main.weight` instead of a list), or frontmatter where a YAML flow scalar replaces a sequence.","solutions":["Supply a []string, []any, array, or a single string.","If a scalar is intended, wrap it in []string{fmt.Sprint(v)}.","Validate Kind==Slice/Array/String before calling."],"exampleFix":"// before\ntypes.ToStringSlicePreserveStringE(42)\n// after\ntypes.ToStringSlicePreserveStringE([]string{\"42\"})","handlingStrategy":"type-guard","validationCode":"func isStringSliceable(v any) bool {\n  if v == nil { return true }\n  switch reflect.ValueOf(v).Kind() {\n  case reflect.String, reflect.Slice, reflect.Array: return true\n  }\n  return false\n}","typeGuard":"func isStringSliceable(v any) bool { k := reflect.ValueOf(v).Kind(); return k==reflect.String||k==reflect.Slice||k==reflect.Array }","tryCatchPattern":null,"preventionTips":["Wrap scalars in []string{fmt.Sprint(v)} before conversion.","Type-check config list fields at decode time.","Avoid passing structs/maps to ToStringSlicePreserveStringE."],"tags":["types","string-slice","convert","config"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}