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

[prototype] typed i18n params #55

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 26 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,32 @@ export type I18NFn<T = any> = {
): () => S extends G ? T[K][G] : string;
};

export type Params = {[key: string]: any};
// 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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавляется такая небольшая утилитка для поиска параметров в строке


export type unstable_I18NFn<T = any> = {
<K extends keyof T, G extends keyof T[K], S extends string>(keysetName: K, key: G | NoEnumLikeStringLiteral<S>, params?: Params<T[K][G]>): string;
keyset<K extends keyof T>(keysetName: K): <G extends keyof T[K], S extends string>(key: G | NoEnumLikeStringLiteral<S>, params?: Params<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?: Params<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?: Params<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?: Params<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?: Params<T[K][G]>): () => string;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут меняется только возвращаемое значение оно всегда string

};

type StringKey = string;

export type Params<K extends (StringKey | PluralValue)> = Record<
K extends string
? KeyParam<K>
: KeyParam<K['zero']> | KeyParam<K['one']> | KeyParam<K['two']> | KeyParam<K['few']> | KeyParam<K['many']> | KeyParam<K['other']>
, any
>;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и типизируются параметры


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

Expand Down
Loading