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

Typescript - Support context #2006

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 19 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,17 +835,25 @@ type ParseKeysByFallbackNs<Keys extends $Dictionary> = _FallbackNamespace extend
? Keys[UnionFallbackNs]
: Keys[_FallbackNamespace & string];

type FilterKeysByContext<Keys, TOpt extends TOptions> = TOpt['context'] extends string
? Keys extends `${infer Prefix}_${TOpt['context']}${infer Suffix}`
? `${Prefix}${Suffix}`
: never
: Keys;

export type ParseKeys<
Ns extends Namespace = _DefaultNamespace,
TOpt extends TOptions = {},
KPrefix = undefined,
Keys extends $Dictionary = KeysByTOptions<TOpt>,
ActualNS extends Namespace = NsByTOptions<Ns, TOpt>,
> = $IsResourcesDefined extends true
?
? FilterKeysByContext<
| ParseKeysByKeyPrefix<Keys[$FirstNamespace<ActualNS>], KPrefix>
| ParseKeysByNamespaces<ActualNS, Keys>
| ParseKeysByFallbackNs<Keys>
| ParseKeysByFallbackNs<Keys>,
TOpt
>
: string;

/*********************************************************
Expand Down Expand Up @@ -879,15 +887,20 @@ type TReturnOptionalObjects<TOpt extends TOptions> = _ReturnObjects extends true
: string;
type DefaultTReturn<TOpt extends TOptions> = TReturnOptionalObjects<TOpt> | TReturnOptionalNull;

type KeyWithContext<Key, TOpt extends TOptions> = TOpt['context'] extends string
? `${Key & string}_${TOpt['context']}`
: Key;

export type TFunctionReturn<
Ns extends Namespace,
Key,
TOpt extends TOptions,
ActualNS extends Namespace = NsByTOptions<Ns, TOpt>,
ActualKey = KeyWithContext<Key, TOpt>,
> = $IsResourcesDefined extends true
? Key extends `${infer Nsp}${_NsSeparator}${infer RestKey}`
? ActualKey extends `${infer Nsp}${_NsSeparator}${infer RestKey}`
? ParseTReturn<RestKey, Resources[Nsp & keyof Resources]>
: ParseTReturn<Key, Resources[$FirstNamespace<ActualNS>]>
: ParseTReturn<ActualKey, Resources[$FirstNamespace<ActualNS>]>
: DefaultTReturn<TOpt>;

type TFunctionReturnOptionalDetails<Ret, TOpt extends TOptions> = TOpt['returnDetails'] extends true
Expand All @@ -910,8 +923,8 @@ export interface TFunction<Ns extends Namespace = _DefaultNamespace, KPrefix = u
>(
...args:
| [key: Key | Key[], options?: TOpt & InterpolationMap<Ret>]
| [key: string | string[], options: TOpt & InterpolationMap<Ret> & { defaultValue: string }]
| [key: string | string[], defaultValue: string, options?: TOpt & InterpolationMap<Ret>]
| [key: string | string[], options: TOpt & $Dictionary & { defaultValue: string }]
| [key: string | string[], defaultValue: string, options?: TOpt & $Dictionary]
): TFunctionReturnOptionalDetails<Ret, TOpt>;
}

Expand Down
1 change: 1 addition & 0 deletions test/typescript/custom-types/i18next.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare module 'i18next' {
foo_other: 'foo';
};
ctx: {
foo: 'foo';
dessert_cake: 'a nice cake';
dessert_muffin_one: 'a nice muffin';
dessert_muffin_other: '{{count}} nice muffins';
Expand Down
17 changes: 11 additions & 6 deletions test/typescript/custom-types/t.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ function i18nextTUsage() {
i18next.t('bar', { ns: 'custom', defaultValue: 'some default value' });
i18next.t('bar', { defaultValue: 'some default value' });
i18next.t('bar', 'some default value');

const str: string = i18next.t('unknown-ns:unknown-key', 'default value');
}

function expectErrorWhenInvalidKeyWithI18nextT() {
Expand Down Expand Up @@ -145,13 +147,16 @@ function nullTranslations() {
// i18next.t('nullKey').trim();
}

// function i18nextContextUsage(t: TFunction<'ctx'>) {
// t('dessert', { context: 'cake' }).trim();
// t('dessert', { context: 'muffin' }).trim();
function i18nextContextUsage(t: TFunction<'ctx'>) {
t('dessert', { context: 'cake' as const }).trim();

// context + plural
t('dessert', { context: 'muffin' as const, count: 3 }).trim();

// // context + plural
// t('dessert', { context: 'muffin', count: 3 }).trim();
// }
// @ts-expect-error
// valid key with invalid context
t('foo', { context: 'cake' as const }).trim();
}

function expectErrorsForDifferentTFunctions(
t1: TFunction<'ord'>,
Expand Down