Validate a data structure
Inspect a key before issuing type-specific commands.
Redis · Module
TYPE reports the Redis data type stored at a key. It returns none when the key does not exist.
TYPE examines the value associated with one key and returns its Redis data-type name. If the key is absent, it returns none; otherwise the documented result can identify strings, lists, sets, sorted sets, hashes, streams, or vector sets.
TYPE key| Name | Type | Description |
|---|---|---|
key | key | The name of the key whose value type should be returned. |
Simple string reply: the type of key, or none when key does not exist.
redis-cli <<'EOF'
SET profile:name Ada
HSET profile:stats visits 3
TYPE profile:name
TYPE profile:stats
TYPE profile:missing
EOFCreates a string and a hash, then reports each stored value's Redis type or none for the absent key.
Inspect a key before issuing type-specific commands.
Diagnose unexpected values in a shared keyspace.
Distinguish missing cache entries from populated entries.
TYPE takes one key and returns a simple string naming the stored value's type. If the key does not exist, the result is none.
Follow-up: How would you distinguish an absent key from a key holding a string? Check whether TYPE returns none; string indicates an existing string value.
The command syntax is TYPE key. The required key argument is the name of the key whose value should be inspected.
Follow-up: What should be done when the key name is dynamic? Pass the key as a properly encoded command argument and interpret the returned type string.
TYPE can report string, list, set, zset, hash, stream, or vectorset. It is useful before selecting commands that operate on a particular Redis data structure.
Follow-up: Why must command selection still be validated after inspection? The key's type can change between separate commands, so atomic or error-handling strategies may be needed.
EXISTSSCANDELJSON.TYPE