Skip to content

Commit

Permalink
types: use unknown to better infer types on nested t functions (#2018)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalexiei committed Sep 7, 2023
1 parent 9584604 commit 5fa7e22
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
8 changes: 8 additions & 0 deletions test/typescript/custom-types/t.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,11 @@ function expectErrorsForDifferentTFunctions(
fn(t2);
fn(t3); // no error
}

function usingTFunctionInsideAnotherTFunction(t: TFunction) {
t('foo', { defaultValue: t('bar') });

t('foo', { something: t('bar') });

t('foo', { defaultValue: t('bar'), something: t('bar') });
}
8 changes: 8 additions & 0 deletions test/typescript/t.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,11 @@ function interpolation(t: TFunction) {
function nullTranslations() {
i18next.t('nullKey').trim();
}

function usingTFunctionInsideAnotherTFunction(t: TFunction) {
t('foobar', { defaultValue: t('inter') });

t('foobar', { defaultValue: t('inter'), someValue: t('inter') });

t('foobar', { something: t('inter') });
}
2 changes: 1 addition & 1 deletion typescript/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type $MergeBy<T, K> = Omit<T, keyof K> & K;
export type $Dictionary<T = any> = { [key: string]: T };
export type $Dictionary<T = unknown> = { [key: string]: T };
export type $OmitArrayKeys<Arr> = Arr extends readonly any[] ? Omit<Arr, keyof any[]> : Arr;
export type $PreservedValue<Value, Fallback> = [Value] extends [never] ? Fallback : Value;
export type $SpecialObject = object | Array<string | object>;
2 changes: 1 addition & 1 deletion typescript/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ export interface TOptionsBase {
/**
* Default value to return if a translation was not found
*/
defaultValue?: any;
defaultValue?: unknown;
/**
* Count value used for plurals
*/
Expand Down
6 changes: 5 additions & 1 deletion typescript/t.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ type ParseInterpolationValues<Ret> =
| (Value extends `${infer ActualValue},${string}` ? ActualValue : Value)
| ParseInterpolationValues<Rest>
: never;
type InterpolationMap<Ret> = Record<$PreservedValue<ParseInterpolationValues<Ret>, string>, any>;
type InterpolationMap<Ret> = Record<
$PreservedValue<ParseInterpolationValues<Ret>, string>,
unknown
>;

type ParseTReturnPlural<
Res,
Expand Down Expand Up @@ -208,6 +211,7 @@ type AppendKeyPrefix<Key, KPrefix> = KPrefix extends string
**************************/
export interface TFunction<Ns extends Namespace = DefaultNamespace, KPrefix = undefined> {
$TFunctionBrand: $IsResourcesDefined extends true ? `${$FirstNamespace<Ns>}` : never;

<
const Key extends ParseKeys<Ns, TOpt, KPrefix> | TemplateStringsArray,
const TOpt extends TOptions,
Expand Down

0 comments on commit 5fa7e22

Please sign in to comment.