Skip to main content

Description

Returns the string representation of the type of the value stored at key. The different types that can be returned are: string, list, set, zset, hash, and none.

Method Signature

type(key: string): Promise<Type>

Parameters

key
string
required
The key to get the type of

Return Value

result
Type
The type of the key, one of:
  • "string" - String value
  • "list" - List
  • "set" - Set
  • "zset" - Sorted set
  • "hash" - Hash
  • "none" - Key does not exist

Examples

String type

await redis.set("mykey", "value");
const type = await redis.type("mykey");
console.log(type); // "string"

List type

await redis.lpush("mylist", "item");
const type = await redis.type("mylist");
console.log(type); // "list"

Hash type

await redis.hset("myhash", { field: "value" });
const type = await redis.type("myhash");
console.log(type); // "hash"

Set type

await redis.sadd("myset", "member");
const type = await redis.type("myset");
console.log(type); // "set"

Sorted set type

await redis.zadd("myzset", { score: 1, member: "member" });
const type = await redis.type("myzset");
console.log(type); // "zset"

Non-existent key

const type = await redis.type("nonexistent");
console.log(type); // "none"

Type-specific operations

const type = await redis.type("mykey");

switch (type) {
  case "string":
    const value = await redis.get("mykey");
    console.log(value);
    break;
  case "list":
    const items = await redis.lrange("mykey", 0, -1);
    console.log(items);
    break;
  case "hash":
    const fields = await redis.hgetall("mykey");
    console.log(fields);
    break;
  case "none":
    console.log("Key does not exist");
    break;
  default:
    console.log(`Key is of type: ${type}`);
}

See Also

  • exists - Check if a key exists
  • scan - Scan with type filtering