I am getting this error when I deploy my simple Next.js app to Vercel (and only on deploy, locally it never has this error):
Type error: Type instantiation is excessively deep and possibly infinite.
When I run pnpm build
locally it builds fine, but on Vercel it is failing I’d say 70% of the time (sometimes it does successfully deploy).
How can I better debug this, perhaps locally, or just on Vercel? How can I narrow down what the culprit might be?
I tried adding this prop to my tsconfig.json
but it didn’t seem to do anything:
{
"compilerOptions": {
"maxNodeModuleJsDepth": 1000
...
}
}
The only thing I can think of in my app where I manually used recursion in TypeScript is here:
type RecursivelyReplaceNullWithUndefined<T> = T extends null
? undefined
: T extends Date
? T
: {
[K in keyof T]: T[K] extends (infer U)[]
? RecursivelyReplaceNullWithUndefined<U>[]
: RecursivelyReplaceNullWithUndefined<T[K]>
}
Used in this function:
import isArray from 'lodash/isArray'
import isObject from 'lodash/isObject'
export function removeNulls<T>(
obj: T,
): RecursivelyReplaceNullWithUndefined<T> {
const res = {} as any
for (const name in obj) {
let value = obj[name] as any
if (isArray(value)) {
value = value.map(removeNulls)
} else if (isObject(value)) {
value = removeNulls(value)
} else if (value == null) {
value = undefined
}
res[name] = value
}
return res
}
Which is used to cleanup database query nulls:
export async function findScript({ key }: { key: string }) {
const script = await db
.selectFrom('script')
.selectAll()
.where('slug', '=', key)
.executeTakeFirstOrThrow()
return removeNulls({
id: script.id,
slug: script.slug,
name: script.name,
category: script.category,
is_rtl: script.is_rtl,
is_vertical: script.is_vertical,
iso_15924: script.iso_15924,
iso_15924_numeric: script.iso_15924_numeric,
})
}
And that is used in the Next.js route.ts
file.
1