TanStackT
TanStack11mo ago
10 replies
specific-silver

Understanding the default error type in v5.

I can throw anything, i.e. throw 5. I log the error and is indeed 5 with type number but the error type is annotated as Error | null. This can introduce runtime errors when narrowing the union and performing operations on the value such as

// jobQuery.error is Error | null;
if (jobQuery.error === null) return
const message = jobQuery.error.message.toLocaleLowerCase() // Runtime crash because it's a number, not a string


Is this intentional? Maybe my configuration is wrong or I'm doing something wrong. The old unknown type avoided the problem by typing errors in the most generic way possible.
Was this page helpful?