Skip to content
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

feat: I18n strict params #57

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

type NoEnumLikeStringLiteral<T> = string extends T ? T : never;

export type I18NFn<T = any> = {

Check warning on line 7 in src/types.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
<K extends keyof T, G extends keyof T[K], S extends string>(
keysetName: K,
key: G | NoEnumLikeStringLiteral<S>,
params?: {[key: string]: any},

Check warning on line 11 in src/types.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
): S extends G ? T[K][G] : string;
keyset<K extends keyof T>(
keysetName: K,
): <G extends keyof T[K], S extends string>(
key: G | NoEnumLikeStringLiteral<S>,
params?: {[key: string]: any},

Check warning on line 17 in src/types.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
) => S extends G ? T[K][G] : string;
i18n<K extends keyof T, G extends keyof T[K], S extends string>(
keysetName: K,
Expand All @@ -25,14 +25,14 @@
key: string
): () => boolean;
bind(
thisArg: any,

Check warning on line 28 in src/types.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
): <K extends keyof T, G extends keyof T[K], S extends string>(
keysetName: K,
key: G | NoEnumLikeStringLiteral<S>,
params?: {[key: string]: any},

Check warning on line 32 in src/types.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
) => S extends G ? T[K][G] : string;
bind<K extends keyof T>(
thisArg: any,

Check warning on line 35 in src/types.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
keysetName: K,
): <G extends keyof T[K], S extends string>(
key: G | NoEnumLikeStringLiteral<S>,
Expand All @@ -51,6 +51,75 @@
): () => 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
Loading