From 37c68769acd007762e355d3fcddcf1cd4407d999 Mon Sep 17 00:00:00 2001 From: itwillwork Date: Wed, 13 Nov 2024 15:29:00 +0300 Subject: [PATCH] i18n strict params --- src/types.ts | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/types.ts b/src/types.ts index f29f5f3..2a32576 100644 --- a/src/types.ts +++ b/src/types.ts @@ -51,6 +51,71 @@ export type I18NFn = { ): () => S extends G ? T[K][G] : string; }; +// Recursive helper for finding path parameters +type KeyParam = + Path extends `${infer L}{{${infer K}}}${infer R}` + ? K | KeyParam | KeyParam + : never; + +type StringKey = string; + +type RequiredPluralValue = { + count: number | string; // "string" parsed via Number constructor +} + +export type StrictTypedParams = ( + K extends string + ? Record, V> + : Record | KeyParam | KeyParam | KeyParam | KeyParam | KeyParam, V> & RequiredPluralValue +); + +export type StrictTypedParamsI18NFn = { + ( + keysetName: K, + key: G | NoEnumLikeStringLiteral, + params?: StrictTypedParams, + ): S extends G ? T[K][G] : string; + keyset( + keysetName: K, + ): ( + key: G | NoEnumLikeStringLiteral, + params?: StrictTypedParams, + ) => S extends G ? T[K][G] : string; + i18n( + keysetName: K, + key: G | NoEnumLikeStringLiteral, + ): () => S extends G ? T[K][G] : string; + has( + keysetName: K, + key: string + ): () => boolean; + bind( + thisArg: any, + ): ( + keysetName: K, + key: G | NoEnumLikeStringLiteral, + params?: StrictTypedParams, + ) => S extends G ? T[K][G] : string; + bind( + thisArg: any, + keysetName: K, + ): ( + key: G | NoEnumLikeStringLiteral, + params?: StrictTypedParams, + ) => S extends G ? T[K][G] : string; + bind( + thisArg: any, + keysetName: K, + key: G | NoEnumLikeStringLiteral, + ): (params?: StrictTypedParams) => S extends G ? T[K][G] : string; + bind( + thisArg: any, + keysetName: K, + key: G | NoEnumLikeStringLiteral, + params?: StrictTypedParams, + ): () => S extends G ? T[K][G] : string; +}; + export type Params = {[key: string]: any}; export type Pluralizer = (count: number, pluralForms: typeof PluralForm) => PluralForm;