Skip to content

Commit

Permalink
fix(i18next): add overloaded methods and fix tsdoc for those
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed May 7, 2024
1 parent 13aa0b1 commit 5fc4ae9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
60 changes: 60 additions & 0 deletions packages/i18next/src/lib/InternationalizationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,66 @@ export class InternationalizationHandler {
Ns extends Namespace = DefaultNamespace,
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, undefined>, TOpt> = TOpt['returnObjects'] extends true ? $SpecialObject : string,
const ActualOptions extends TOpt & InterpolationMap<Ret> = TOpt & InterpolationMap<Ret>
>(locale: string, key: Key | Key[], options?: ActualOptions): TFunctionReturnOptionalDetails<Ret, TOpt>;

/**
* Localizes a content given one or more keys and i18next options.
* @since 2.0.0
* @param locale The language to be used.
* @param key The key or keys to retrieve the content from.
* @param options The interpolation options as well as a `defaultValue` for the key and any key/value pairs.
* @see {@link https://www.i18next.com/overview/api#t}
* @returns The localized content.
*/
public format<
const Key extends ParseKeys<Ns, TOpt, undefined>,
const TOpt extends TOptions = TOptions,
Ns extends Namespace = DefaultNamespace,
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, undefined>, TOpt> = TOpt['returnObjects'] extends true ? $SpecialObject : string,
const ActualOptions extends TOpt & InterpolationMap<Ret> = TOpt & InterpolationMap<Ret>
>(locale: string, key: string | string[], options: TOpt & $Dictionary & { defaultValue: string }): TFunctionReturnOptionalDetails<Ret, TOpt>;

/**
* Localizes a content given one or more keys and i18next options.
* @since 2.0.0
* @param locale The language to be used.
* @param key The key or keys to retrieve the content from.
* @param defaultValue The default value to use if the key is not found.
* @param options The interpolation options.
* @see {@link https://www.i18next.com/overview/api#t}
* @returns The localized content.
*/
public format<
const Key extends ParseKeys<Ns, TOpt, undefined>,
const TOpt extends TOptions = TOptions,
Ns extends Namespace = DefaultNamespace,
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, undefined>, TOpt> = TOpt['returnObjects'] extends true ? $SpecialObject : string,
const ActualOptions extends TOpt & InterpolationMap<Ret> = TOpt & InterpolationMap<Ret>
>(
locale: string,
key: string | string[],
defaultValue: string | undefined,
options?: TOpt & $Dictionary
): TFunctionReturnOptionalDetails<Ret, TOpt>;

/**
* Localizes a content given one or more keys and i18next options.
* @since 2.0.0
* @param locale The language to be used.
*
* @remark This function also has additional parameters for `key`, `defaultValue`, and `options`, however
* TSDoc does not let us document those while matching proper implementation. See the overloads for this method
* for the documentation on those parameters.
*
* @see {@link https://www.i18next.com/overview/api#t}
* @returns The localized content.
*/
public format<
const Key extends ParseKeys<Ns, TOpt, undefined>,
const TOpt extends TOptions = TOptions,
Ns extends Namespace = DefaultNamespace,
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, undefined>, TOpt> = TOpt['returnObjects'] extends true ? $SpecialObject : string,
const ActualOptions extends TOpt & InterpolationMap<Ret> = TOpt & InterpolationMap<Ret>
>(
locale: string,
...[key, defaultValueOrOptions, optionsOrUndefined]:
Expand Down
52 changes: 52 additions & 0 deletions packages/i18next/src/lib/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,58 @@ export async function resolveKey<
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, undefined>, TOpt> = TOpt['returnObjects'] extends true ? $SpecialObject : string,
Ns extends Namespace = DefaultNamespace,
const ActualOptions extends TOpt & InterpolationMap<Ret> = TOpt & InterpolationMap<Ret>
>(target: Target, key: Key | Key[], options?: ActualOptions): Promise<TFunctionReturnOptionalDetails<Ret, TOpt>>;

/**
* Resolves a key and its parameters.
* @since 2.0.0
* @param target The target to fetch the language key from.
* @param key The i18next key.
* @param options The interpolation options as well as a `defaultValue` for the key and any key/value pairs.
* @returns The data that `key` held, processed by i18next.
*/
export async function resolveKey<
const Key extends ParseKeys<Ns, TOpt, undefined>,
const TOpt extends TOptions = TOptions,
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, undefined>, TOpt> = TOpt['returnObjects'] extends true ? $SpecialObject : string,
Ns extends Namespace = DefaultNamespace,
const ActualOptions extends TOpt & InterpolationMap<Ret> = TOpt & InterpolationMap<Ret>
>(target: Target, key: string | string[], options: TOpt & $Dictionary & { defaultValue: string }): Promise<TFunctionReturnOptionalDetails<Ret, TOpt>>;

/**
* Resolves a key and its parameters.
* @since 2.0.0
* @param target The target to fetch the language key from.
* @param key The i18next key.
* @param defaultValue The default value to use if the key is not found.
* @param options The interpolation options.
* @returns The data that `key` held, processed by i18next.
*/
export async function resolveKey<
const Key extends ParseKeys<Ns, TOpt, undefined>,
const TOpt extends TOptions = TOptions,
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, undefined>, TOpt> = TOpt['returnObjects'] extends true ? $SpecialObject : string,
Ns extends Namespace = DefaultNamespace,
const ActualOptions extends TOpt & InterpolationMap<Ret> = TOpt & InterpolationMap<Ret>
>(target: Target, key: string | string[], defaultValue: string, options?: TOpt & $Dictionary): Promise<TFunctionReturnOptionalDetails<Ret, TOpt>>;

/**
* Resolves a key and its parameters.
* @since 2.0.0
* @param target The target to fetch the language key from.
*
* @remark This function also has additional parameters for `key`, `defaultValue`, and `options`, however
* TSDoc does not let us document those while matching proper implementation. See the overloads for this method
* for the documentation on those parameters.
*
* @returns The data that `key` held, processed by i18next.
*/
export async function resolveKey<
const Key extends ParseKeys<Ns, TOpt, undefined>,
const TOpt extends TOptions = TOptions,
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, undefined>, TOpt> = TOpt['returnObjects'] extends true ? $SpecialObject : string,
Ns extends Namespace = DefaultNamespace,
const ActualOptions extends TOpt & InterpolationMap<Ret> = TOpt & InterpolationMap<Ret>
>(
target: Target,
...[key, defaultValueOrOptions, optionsOrUndefined]:
Expand Down

0 comments on commit 5fc4ae9

Please sign in to comment.