From b5903e959435fe9dc4fc441596a06b294744f885 Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Sun, 22 Feb 2026 23:43:50 +0100 Subject: [PATCH] Lang: Remove some duplication in types (#6712) --- src/util/localization/types.ts | 58 +++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/util/localization/types.ts b/src/util/localization/types.ts index 44493eac1..1f41cebdb 100644 --- a/src/util/localization/types.ts +++ b/src/util/localization/types.ts @@ -47,6 +47,27 @@ export type AdvancedLangFnOptions = AdvancedLangFnOptionsRegular | AdvancedLangF type LangPairWithNodes = LangPairWithVariables; type LangPairPluralWithNodes = LangPairPluralWithVariables; +// Helpers for merged overloads +type AllKeysOf = T extends any ? keyof T : never; + +type AnyLangOptions = LangFnOptions | AdvancedLangFnOptions; +type AnyLangPluralOptions = LangFnOptionsWithPlural | AdvancedLangFnOptionsWithPlural; + +// Maps unknown option keys to `never`, catching typos even through generics +type StrictLangOptions = O & Record>, never>; + +type LangFnReturnType = O extends AdvancedLangFnOptions ? TeactNode : string; + +type PrepVariablesType = + O extends AdvancedLangFnOptions + ? LangPairWithNodes[K] + : LangPairWithVariables[K]; + +type PrepPluralVariablesType = + O extends AdvancedLangFnOptions + ? LangPairPluralWithNodes[K] + : LangPairPluralWithVariables[K]; + type RegularLangFnParametersWithoutVariables = { key: RegularLangKey; variables?: undefined; @@ -122,31 +143,18 @@ export type AdvancedLangFnParameters = export type LangFnParameters = RegularLangFnParameters | AdvancedLangFnParameters; export type LangFn = { - ( - key: K, variables?: undefined, options?: LangFnOptions, - ): string; - ( - key: K, variables: undefined, options: LangFnOptionsWithPlural, - ): string; - ( - key: K, variables: LangPairWithVariables[K], options?: LangFnOptions, - ): string; - ( - key: K, variables: LangPairPluralWithVariables[K], options: LangFnOptionsWithPlural, - ): string; - - ( - key: K, variables?: undefined, options?: AdvancedLangFnOptions, - ): TeactNode; - ( - key: K, variables: undefined, options: AdvancedLangFnOptionsWithPlural, - ): TeactNode; - ( - key: K, variables: LangPairWithVariables[K], options: AdvancedLangFnOptions, - ): TeactNode; - ( - key: K, variables: LangPairPluralWithVariables[K], options: AdvancedLangFnOptionsWithPlural, - ): TeactNode; + ( + key: K, variables: PrepPluralVariablesType, options: StrictLangOptions, + ): LangFnReturnType; + ( + key: K, variables: PrepVariablesType, options?: StrictLangOptions, + ): LangFnReturnType; + ( + key: K, variables: undefined, options: StrictLangOptions, + ): LangFnReturnType; + ( + key: K, variables?: undefined, options?: StrictLangOptions, + ): LangFnReturnType; with: (params: LangFnParameters) => TeactNode; withRegular: (params: RegularLangFnParameters) => string;