Skip to content

introduce returnResolved t option that pratically exposes the result of the resolve function #1764

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

Merged
merged 11 commits into from
May 6, 2022
Prev Previous commit
Next Next commit
TFunctionResolvedResult can be string or object
  • Loading branch information
adrai committed May 6, 2022
commit 624283c96341d0d71bd88e88b05ef7b9811daecd
12 changes: 10 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,9 @@ export interface WithT {
/**
* Object returned from t() function when passed returnResolved: true option.
*/
export type TFunctionResolvedResult = {
export type TFunctionResolvedResult<T = string> = {
usedKey: string;
res: TFunctionResult;
res: T;
exactUsedKey: string;
usedLng: string;
usedNS: string;
Expand All @@ -713,6 +713,14 @@ export interface TFunction {
>(
key: TKeys | TKeys[],
): TResult;
<
TResult extends TFunctionResult = TFunctionResolvedResult<object>,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap,
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap> & { returnResolved: true; returnObjects: true },
): TResult;
<
TResult extends TFunctionResult = TFunctionResolvedResult,
TKeys extends TFunctionKeys = string,
Expand Down
1 change: 1 addition & 0 deletions test/typescript/t.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ function interpolation(t: TFunction) {

const resolved = t('key', { returnResolved: true });
resolved.res;
resolved.res.substring(2, 1);
resolved.usedKey;
resolved.exactUsedKey;
resolved.usedNS;
Expand Down