Skip to content

Commit

Permalink
feat: I18n strict params (#57)
Browse files Browse the repository at this point in the history
* feat: i18n strict params

* feat: rename

* fix: count is number
  • Loading branch information
itwillwork authored Nov 18, 2024
1 parent 8b44fa2 commit 60608b3
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,75 @@ export type I18NFn<T = any> = {
): () => S extends G ? T[K][G] : string;
};

// Recursive helper for finding path parameters
type KeyParam<Path extends string> =
Path extends `${infer L}{{${infer K}}}${infer R}`
? K | KeyParam<L> | KeyParam<R>
: never;

type StringKey = string;

type RequiredPluralValue = {
count: number;
}

export type TypedParams<K = (StringKey | PluralValue), V = string | number> = (
K extends StringKey
? Record<KeyParam<K>, V>
: (
K extends PluralValue
? Record<KeyParam<NonNullable<K["zero"]>> | KeyParam<NonNullable<K['one']>> | KeyParam<NonNullable<K['two']>> | KeyParam<NonNullable<K['few']>> | KeyParam<NonNullable<K['many']>> | KeyParam<NonNullable<K['other']>>, V> & RequiredPluralValue
: unknown
)
);

export type TypedParamsI18NFn<T = any> = {
<K extends keyof T, G extends keyof T[K], S extends string>(
keysetName: K,
key: G | NoEnumLikeStringLiteral<S>,
params?: TypedParams<T[K][G]>,
): string;
keyset<K extends keyof T>(
keysetName: K,
): <G extends keyof T[K], S extends string>(
key: G | NoEnumLikeStringLiteral<S>,
params?: TypedParams<T[K][G]>,
) => string;
i18n<K extends keyof T, G extends keyof T[K], S extends string>(
keysetName: K,
key: G | NoEnumLikeStringLiteral<S>,
): () => string;
has<K extends keyof T>(
keysetName: K,
key: string
): () => boolean;
bind(
thisArg: any,
): <K extends keyof T, G extends keyof T[K], S extends string>(
keysetName: K,
key: G | NoEnumLikeStringLiteral<S>,
params?: TypedParams<T[K][G]>,
) => string;
bind<K extends keyof T>(
thisArg: any,
keysetName: K,
): <G extends keyof T[K], S extends string>(
key: G | NoEnumLikeStringLiteral<S>,
params?: TypedParams<T[K][G]>,
) => string;
bind<K extends keyof T, G extends keyof T[K], S extends string>(
thisArg: any,
keysetName: K,
key: G | NoEnumLikeStringLiteral<S>,
): (params?: TypedParams<T[K][G]>) => string;
bind<K extends keyof T, G extends keyof T[K], S extends string>(
thisArg: any,
keysetName: K,
key: G | NoEnumLikeStringLiteral<S>,
params?: TypedParams<T[K][G]>,
): () => string;
};

export type Params = {[key: string]: any};

export type Pluralizer = (count: number, pluralForms: typeof PluralForm) => PluralForm;
Expand Down

0 comments on commit 60608b3

Please sign in to comment.