-
-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tsc: OOM when using large number of arguments #695
Comments
Hi @ridem, I know about the problem and was waiting for someone to file an issue to see if it is actually a problem that occurs in practice. I think the most convenient way to improve this is to output a type without a permutation in cases where
Instead of a primitive string I would go for a type that restricts the parameter names, but allows any arbitrary combination of those:
const message1: T<'a', 'b'>` = 'hello {a} and {b}' // correct
const message2: T<'a', 'b'>` = 'hello {b} and {b}' // correct
const message3: T<'a', 'b'>` = 'hello {a} and {a}' // correct
const message3: T<'a', 'b'>` = 'hello {c} and {d}' // incorrect This would make sure to offer the best possible typesafety, without running into the OOM issue. Also this line needs to change to import the correct helper type:
It would be great if you could contribute a PR :) |
For those who can't wait for this to be fixed in this branch (RIP Ivan, thanks so much for all your contributions), you can use patch-package to work around this. I didn't care at all about type safety when providing translations in other languages, so I just patched out that whole feature. You may be able to find a more nuanced fix, like what Ivan suggested above. But my patch was simply:
diff --git a/node_modules/typesafe-i18n/types/runtime/src/core.d.mts b/node_modules/typesafe-i18n/types/runtime/src/core.d.mts
index b8c7f07..3128efa 100644
--- a/node_modules/typesafe-i18n/types/runtime/src/core.d.mts
+++ b/node_modules/typesafe-i18n/types/runtime/src/core.d.mts
@@ -48,7 +48,9 @@ export interface ImportLocaleMapping extends LocaleMappingBase {
type Permutation<T extends string, U extends string = T> = [T] extends [never] ? Array<string> : T extends U ? [T, ...Permutation<Exclude<U, T>>] : [T];
type WrapParam<P> = P extends string ? `{${P}}` : never;
type ConstructString<Params extends unknown[]> = Params extends [] ? `${string}` : Params extends [infer Param, ...infer Rest] ? `${string}${WrapParam<Param>}${ConstructString<Rest>}` : Params extends string ? Params : `${string}`;
-export type RequiredParams<Params extends string> = ConstructString<Permutation<Params>>;
+// patch-package START
+export type RequiredParams<Params extends string> = string; // Was `= ConstructString<Permutation<Params>>;`
+// patch-package END
export declare const isPluralPart: (part: Part) => part is PluralPart;
export declare const translate: (textParts: Part[], pluralRules: Intl.PluralRules, formatters: BaseFormatters, args: Arguments) => LocalizedString;
type GetArg<Arg extends string, Type> = Arg extends '' ? void : Arg extends `${infer OptionalArg}?` ? Partial<Record<OptionalArg, Type | undefined>> : Record<Arg, Type>; i.e. I just changed |
Version
5.24.2
Describe the bug
Note: Happy to submit a PR for this issue
The utility type
RequiredParams
, with its use ofPermutation
, makes our Typescript server crash due to OOM issues.RequiredParams is fine as long as the number of arguments is OK, but when we've introduced a multiline string with 8 arguments in it, the number of permutations exploded to
8!=40320
, and tsc crashed because it ran out of memory.Here is the diagnostics information from our
i18n
package, without any external dependency.tsc --diagnostics
:tsc --diagnostics
, withRequiredParams<...> | string
replaced bystring
:One way to solve it is to add a config to allow disabling the use of
RequiredParams
. The code to switch it on/off is already there, but basically depends on the TS version.For now we've just patched the generator code to always spit out
"string"
instead ofgenerateTranslationType(args)
and this suits us.Reproduction
On any sample project:
npm run typesafe-i18n
tsc --diagnostics
. Write down theSymbols
,Types
,Instantiations
,Memory used
tsc --diagnostics
again. Notice how the 4 values have explodedLogs
No response
Config
Additional information
No response
The text was updated successfully, but these errors were encountered: