#Argument of type 'string' is not assignable to parameter of type 'Id<"users">'.

4 messages · Page 1 of 1 (latest)

mortal lilyBOT
#

Hi @topaz dome. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The TypeScript error you're seeing is because Convex uses a branded type for document IDs, such as Id<"users">, which is not the same as a plain string. Even though IDs are strings at runtime, Convex's type system enforces that you only use the correct branded type for safety. The error message:

Argument of type 'string' is not assignable to parameter of type 'Id<"users">'.
Type 'string' is not assignable to type '{ __tableName: "users"; }'.

means that subject (which is a string) is being passed where an Id<"users"> is expected.

How to fix

You need to cast the string to the branded type. If you are sure that subject is a valid user ID (i.e., it was generated by Convex for the "users" table), you can cast it like this:

.withIndex("by_userId", (q) => q.eq("userId", subject as Id<"users">))

Similarly, for the assignment:

userId: identity.subject as Id<"users">

This is a common pattern in Convex when you receive an ID as a string (for example, from authentication or from external sources) and need to use it as a Convex document ID. Just be sure that the string really is a valid ID for the table you’re using, as the type system won’t check this at runtime—only at compile time. For extra safety, you can use argument validators like v.id("users") in your Convex functions to ensure the ID is valid at runtime as well.

For more details, see the Convex documentation on Document IDs and the explanation of string branding:

#

a Convex Id<"users"> is the type string & { __tableName: "users" }. This means if I try to assign const foo: Id<"users"> = "bar";, I will get a type error. It's fine to do const foo: string = "bar" as Id<"users">; however, since Id is just a more "refined" type of string.What is string branding?

And for usage examples, see Types and Validators: A Convex Cookbook.

Summary:
Cast your string to the appropriate Id type when passing it to Convex APIs that expect a branded ID.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@topaz dome Kapa.ai is still learning and improving, please let me know how I did by reacting below.