Skip to content

Commit bd9b016

Browse files
committed
types: fix: WithTranslation type from next-i18next is not generic (TS error) #2329
1 parent 74d0c48 commit bd9b016

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 15.4.3
2+
3+
- types: fix: WithTranslation type from next-i18next is not generic (TS error) [#2329](https://github.com/i18next/next-i18next/issues/2329)
4+
15
## 15.4.2
26

37
- types: re-add @types/hoist-non-react-statics to dependencies [#2316](https://github.com/i18next/next-i18next/pull/2316)

src/types.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
3+
import type { WithTranslation, UseTranslation } from './types'
4+
5+
type X = WithTranslation<'common'>
6+
7+
const _assert: X = {
8+
i18n: {} as any,
9+
// t signature is complex; cast to any for this compile-only check
10+
t: ((..._args: any[]) => '') as any,
11+
tReady: true,
12+
}
13+
14+
type R = ReturnType<UseTranslation<'common'>>
15+
16+
const _assertTwo: R = [
17+
// t
18+
((..._args: any[]) => '') as any,
19+
// i18n
20+
{} as any,
21+
// ready
22+
true,
23+
] as any
24+
25+
26+
// Runtime noop test so Jest treats this file as a test suite.
27+
test('types compile-only', () => {
28+
expect(true).toBe(true)
29+
})

src/types.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ import {
66
withTranslation,
77
WithTranslation as ReactI18nextWithTranslation,
88
Translation,
9+
FallbackNs,
10+
UseTranslationOptions,
11+
UseTranslationResponse,
912
} from 'react-i18next'
1013
import {
1114
InitOptions,
1215
i18n as I18NextClient,
1316
TFunction as I18NextTFunction,
1417
TypeOptions,
18+
FlatNamespace,
19+
KeyPrefix,
1520
} from 'i18next'
1621
import { appWithTranslation, i18n } from './'
1722

@@ -58,12 +63,23 @@ export type InternalConfig = Omit<UserConfig, 'i18n'> &
5863
supportedLngs: string[]
5964
}
6065

61-
export type UseTranslation = typeof useTranslation
66+
type $Tuple<T> = readonly [T?, ...T[]];
67+
68+
export type UseTranslation<
69+
Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
70+
KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
71+
> = (
72+
ns?: Ns,
73+
options?: UseTranslationOptions<KPrefix>,
74+
) => UseTranslationResponse<FallbackNs<Ns>, KPrefix>
6275
export type AppWithTranslation = typeof appWithTranslation
6376
export type TFunction = I18NextTFunction
6477
export type I18n = I18NextClient
6578
export type WithTranslationHocType = typeof withTranslation
66-
export type WithTranslation = ReactI18nextWithTranslation
79+
export type WithTranslation<
80+
Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
81+
KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
82+
> = ReactI18nextWithTranslation<Ns, KPrefix>
6783
export type InitPromise = Promise<TFunction>
6884
export type CreateClientReturn = {
6985
i18n: I18n

0 commit comments

Comments
 (0)