Skip to content

Commit 93c8d81

Browse files
author
Tom Lienard
authored
Revert "feat(use-i18n): required generic (#1059)" (#1060)
This reverts commit e028640.
1 parent 2eb53b9 commit 93c8d81

File tree

4 files changed

+17
-50
lines changed

4 files changed

+17
-50
lines changed

packages/use-i18n/src/__tests__/usei18n.tsx

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ type Locale = {
1717
title: 'Welcome on @scaelway/ui i18n hook'
1818
}
1919

20-
type NamespaceLocale = {
21-
name: 'Name'
22-
lastName: 'Last Name'
23-
languages: 'Languages'
24-
}
25-
2620
const wrapper =
2721
({
2822
loadDateLocale = async (locale: string) =>
@@ -98,7 +92,7 @@ describe('i18n hook', () => {
9892
})
9993

10094
it('should use defaultLoad, useTranslation, switch local and translate', async () => {
101-
const { result } = renderHook(() => useTranslation<Locale>([]), {
95+
const { result } = renderHook(() => useTranslation([]), {
10296
wrapper: wrapper({ defaultLocale: 'en' }),
10397
})
10498
// first render there is no load
@@ -136,7 +130,7 @@ describe('i18n hook', () => {
136130
}) => import(`./locales/namespaces/${locale}/${namespace}.json`)
137131

138132
const { result } = renderHook(
139-
() => useTranslation<NamespaceLocale>(['user', 'profile'], load),
133+
() => useTranslation(['user', 'profile'], load),
140134
{
141135
wrapper: wrapper({
142136
defaultLocale: 'en',
@@ -191,16 +185,13 @@ describe('i18n hook', () => {
191185
namespace: string
192186
}) => import(`./locales/namespaces/${locale}/${namespace}.json`)
193187

194-
const { result } = renderHook(
195-
() => useTranslation<NamespaceLocale>(['user'], load),
196-
{
197-
wrapper: wrapper({
198-
defaultLocale: 'fr',
199-
enableDefaultLocale: true,
200-
supportedLocales: ['en', 'fr'],
201-
}),
202-
},
203-
)
188+
const { result } = renderHook(() => useTranslation(['user'], load), {
189+
wrapper: wrapper({
190+
defaultLocale: 'fr',
191+
enableDefaultLocale: true,
192+
supportedLocales: ['en', 'fr'],
193+
}),
194+
})
204195

205196
// current local will be 'en' based on navigator
206197
// await load of locales

packages/use-i18n/src/__typetests__/namespaceTranslation.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/* eslint-disable react-hooks/rules-of-hooks */
21
import { expectError, expectType } from 'tsd-lite'
32
import { useI18n } from '../usei18n'
43

4+
// eslint-disable-next-line react-hooks/rules-of-hooks
55
const { namespaceTranslation } = useI18n<{
66
hello: 'world'
77
'doe.john': 'John Doe'
@@ -50,7 +50,3 @@ expectType<string>(
5050
)
5151
expectError(scopedT3('john', {}))
5252
expectError(scopedT3('john'))
53-
54-
// Required generic
55-
const { namespaceTranslation: namespaceTranslation2 } = useI18n()
56-
expectError(namespaceTranslation2('test'))

packages/use-i18n/src/__typetests__/t.test.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/* eslint-disable react-hooks/rules-of-hooks */
21
import { expectError, expectType } from 'tsd-lite'
3-
import { useI18n, useTranslation } from '../usei18n'
2+
import { useI18n } from '../usei18n'
43

4+
// eslint-disable-next-line react-hooks/rules-of-hooks
55
const { t } = useI18n<{
66
hello: 'world'
77
'doe.john': 'John Doe'
@@ -58,7 +58,3 @@ expectType<string>(
5858
),
5959
}),
6060
)
61-
62-
// Required generic
63-
const { t: t2 } = useI18n()
64-
expectError(t2('test'))

packages/use-i18n/src/usei18n.tsx

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ import type { ReactParamsObject, ScopedTranslateFn, TranslateFn } from './types'
2525
const LOCALE_ITEM_STORAGE = 'locale'
2626

2727
type TranslationsByLocales = Record<string, BaseLocale>
28-
type RequiredGenericContext<Locale extends BaseLocale> =
29-
keyof Locale extends never
30-
? Omit<Context<Locale>, 't' | 'namespaceTranslation'> & {
31-
t: (str: 'You must pass a generic argument to useI18n()') => void
32-
namespaceTranslation: (
33-
str: 'You must pass a generic argument to useI18n()',
34-
) => void
35-
}
36-
: Context<Locale>
3728

3829
const areNamespacesLoaded = (
3930
namespaces: string[],
@@ -108,23 +99,19 @@ interface Context<Locale extends BaseLocale> {
10899
// useI18n / useTranslation requires to explicitely give a Locale to use.
109100
const I18nContext = createContext<Context<any> | undefined>(undefined)
110101

111-
export function useI18n<
112-
// eslint-disable-next-line @typescript-eslint/ban-types
113-
Locale extends BaseLocale = {},
114-
>(): RequiredGenericContext<Locale> {
102+
export function useI18n<Locale extends BaseLocale>(): Context<Locale> {
115103
const context = useContext(I18nContext)
116104
if (context === undefined) {
117105
throw new Error('useI18n must be used within a I18nProvider')
118106
}
119107

120-
return context as unknown as RequiredGenericContext<Locale>
108+
return context as unknown as Context<Locale>
121109
}
122110

123-
// eslint-disable-next-line @typescript-eslint/ban-types
124-
export function useTranslation<Locale extends BaseLocale = {}>(
111+
export function useTranslation<Locale extends BaseLocale>(
125112
namespaces: string[] = [],
126113
load: LoadTranslationsFn | undefined = undefined,
127-
): RequiredGenericContext<Locale> & { isLoaded: boolean } {
114+
): Context<Locale> & { isLoaded: boolean } {
128115
const context = useContext(I18nContext)
129116
if (context === undefined) {
130117
throw new Error('useTranslation must be used within a I18nProvider')
@@ -143,10 +130,7 @@ export function useTranslation<Locale extends BaseLocale = {}>(
143130
[loadedNamespaces, namespaces],
144131
)
145132

146-
return {
147-
...context,
148-
isLoaded,
149-
} as unknown as RequiredGenericContext<Locale> & {
133+
return { ...context, isLoaded } as unknown as Context<Locale> & {
150134
isLoaded: boolean
151135
}
152136
}

0 commit comments

Comments
 (0)