Skip to content

Instantly share code, notes, and snippets.

@natterstefan
Last active September 2, 2022 07:16
Show Gist options
  • Save natterstefan/bc6ce87f85571005ef47017c29a226ba to your computer and use it in GitHub Desktop.
Save natterstefan/bc6ce87f85571005ef47017c29a226ba to your computer and use it in GitHub Desktop.
react-i18next and TS2589: Type instantiation is excessively deep and possibly infinite.

react-i18next and TS2589: Type instantiation is excessively deep and possibly infinite.

visitors

The patch works both with tsc and next build, as described in the comments of the patch.

Inspired by

diff --git a/node_modules/typescript/lib/tsc.js b/node_modules/typescript/lib/tsc.js
index 4964bb7..8fa48e0 100644
--- a/node_modules/typescript/lib/tsc.js
+++ b/node_modules/typescript/lib/tsc.js
@@ -52730,7 +52730,8 @@ var ts;
if (!couldContainTypeVariables(type)) {
return type;
}
- if (instantiationDepth === 100 || instantiationCount >= 5000000) {
+ // increase limit to make i18n type work @see https://github.com/i18next/react-i18next/issues/1417#issuecomment-983951906
+ if (instantiationDepth === 200 || instantiationCount >= 5000000) {
ts.tracing === null || ts.tracing === void 0 ? void 0 : ts.tracing.instant("checkTypes", "instantiateType_DepthLimit", { typeId: type.id, instantiationDepth: instantiationDepth, instantiationCount: instantiationCount });
error(currentNode, ts.Diagnostics.Type_instantiation_is_excessively_deep_and_possibly_infinite);
return errorType;
diff --git a/node_modules/typescript/lib/typescript.js b/node_modules/typescript/lib/typescript.js
index e8734a6..b6b6619 100644
--- a/node_modules/typescript/lib/typescript.js
+++ b/node_modules/typescript/lib/typescript.js
@@ -63632,7 +63632,21 @@ var ts;
if (!couldContainTypeVariables(type)) {
return type;
}
- if (instantiationDepth === 100 || instantiationCount >= 5000000) {
+ /**
+ * increase limit to make i18n type work @see https://github.com/i18next/react-i18next/issues/1417#issuecomment-983951906
+ *
+ * Why?
+ *
+ * Because of how next validates types when running `next build`, we
+ * have to patch not only tsc.js but also this file.
+ *
+ * @see https://github.com/vercel/next.js/blob/4cc7f11da2194aa9ffce4e04b8a0c6ee340f4f85/packages/next/build/index.ts#L176
+ * @see https://github.com/vercel/next.js/blob/4cc7f11da2194aa9ffce4e04b8a0c6ee340f4f85/packages/next/lib/verifyTypeScriptSetup.ts#L20-L24
+ *
+ * And ignoring the build step is not a desired solution.
+ * @see https://nextjs.org/docs/api-reference/next.config.js/ignoring-typescript-errors
+ */
++ if (instantiationDepth === 200 || instantiationCount >= 5000000) {
// We have reached 100 recursive type instantiations, or 5M type instantiations caused by the same statement
// or expression. There is a very high likelyhood we're dealing with a combination of infinite generic types
// that perpetually generate new type identities, so we stop the recursion here by yielding the error type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment