{"id":"ee1d9cd0d0e3a6b5","repo":"go-redis/redis","slug":"redis-unexpected-type-t-for-bool","errorCode":null,"errorMessage":"redis: unexpected type=%T for Bool","messagePattern":"redis: unexpected type=%T for Bool","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command.go","lineNumber":773,"sourceCode":"\nfunc (cmd *Cmd) Bool() (bool, error) {\n\tcmd.await()\n\tif cmd.err != nil {\n\t\treturn false, cmd.err\n\t}\n\treturn toBool(cmd.val)\n}\n\nfunc toBool(val interface{}) (bool, error) {\n\tswitch val := val.(type) {\n\tcase bool:\n\t\treturn val, nil\n\tcase int64:\n\t\treturn val != 0, nil\n\tcase string:\n\t\treturn strconv.ParseBool(val)\n\tdefault:\n\t\terr := fmt.Errorf(\"redis: unexpected type=%T for Bool\", val)\n\t\treturn false, err\n\t}\n}\n\nfunc (cmd *Cmd) Slice() ([]interface{}, error) {\n\tcmd.await()\n\tif cmd.err != nil {\n\t\treturn nil, cmd.err\n\t}\n\tswitch val := cmd.val.(type) {\n\tcase []interface{}:\n\t\treturn val, nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"redis: unexpected type=%T for Slice\", val)\n\t}\n}\n\nfunc (cmd *Cmd) StringSlice() ([]string, error) {","sourceCodeStart":755,"sourceCodeEnd":791,"githubUrl":"https://github.com/go-redis/redis/blob/36d97525cd8076aed67cddf54778e9ea84550929/command.go#L755-L791","documentation":"Returned by toBool (command.go:764) when Cmd.Bool() is called on a reply value that is neither bool, int64, nor string. bool is returned directly; int64 is true if non-zero; string is parsed via strconv.ParseBool; any other type (array, nil, map) yields this error. Means the command did not return a single boolean-like scalar.","triggerScenarios":"Calling Bool() on a raw Cmd holding an array reply; calling Bool() on a nil value when redis.Nil was not surfaced as cmd.err; using Bool() where Exists/BoolSliceCmd is the correct accessor.","commonSituations":"Generic Do Cmd used for a command returning arrays; expecting a bool from a command whose value came back as []interface{}; confusing cmd.Err()==redis.Nil with a present-but-false value.","solutions":["Use the typed command (Exists → BoolCmd; SetNX → BoolCmd; SIsMember → BoolCmd).","Type-switch on cmd.Result() when using Do.","Check cmd.Err() for redis.Nil before reading the value."],"exampleFix":"// before\nok, err := client.Do(ctx, \"EXISTS\", \"k\").Bool()\n\n// after\nok, err := client.Exists(ctx, \"k\").Result()","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func asBool(cmd *redis.Cmd) (bool, error) {\n    if err := cmd.Err(); err != nil { return false, err }\n    switch v := cmd.Val().(type) {\n    case bool:\n        return v, nil\n    case int64:\n        return v != 0, nil\n    case string:\n        return strconv.ParseBool(v)\n    default:\n        return false, fmt.Errorf(\"not a bool: %T\", v)\n    }\n}","tryCatchPattern":"ok, err := cmd.Bool()\nif err != nil && strings.Contains(err.Error(), \"unexpected type\") {\n    // reply was not boolean-like — use Exists/SIsMember/SetNX\n}","preventionTips":["Use BoolCmd-returning methods (Exists, SetNX, SIsMember) for boolean replies.","Distinguish redis.Nil from a present false value before coercing.","Type-switch on Result() for raw Do replies."],"tags":["command","type-conversion","reply-parsing","api-misuse"],"analyzedSha":"36d97525cd8076aed67cddf54778e9ea84550929","analyzedAt":"2026-08-06T01:08:27.376Z","schemaVersion":2}