Skip to content

Commit 1b8b88b

Browse files
author
Tom Lienard
authored
feat(i18n): add types for react components in params value (#1034)
1 parent 589be65 commit 1b8b88b

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,12 @@ describe('i18n hook', () => {
602602
})
603603

604604
it('should work with a component', async () => {
605-
const { result } = renderHook(() => useTranslation([]), {
606-
wrapper: wrapper({ defaultLocale: 'en' }),
607-
})
605+
const { result } = renderHook(
606+
() => useTranslation<{ 'with.identifier': 'Hello {identifier}' }>([]),
607+
{
608+
wrapper: wrapper({ defaultLocale: 'en' }),
609+
},
610+
)
608611
const CustomComponent = ({ children }: { children: ReactNode }) => (
609612
<p style={{ fontWeight: 'bold' }}>{children}</p>
610613
)

packages/use-i18n/src/__typetests__/t.test.ts renamed to packages/use-i18n/src/__typetests__/t.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,14 @@ expectType<string>(
4747
)
4848
expectError(t('describe.john', {}))
4949
expectError(t('describe.john'))
50+
51+
// With react components as param value
52+
expectType<string>(
53+
t('doe.child', {
54+
name: (
55+
<p>
56+
My name is<b>John</b>
57+
</p>
58+
),
59+
}),
60+
)

packages/use-i18n/src/types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ import type {
33
LocaleKeys,
44
LocaleValue,
55
Params,
6-
ParamsObject,
76
ScopedValue,
87
Scopes,
98
} from 'international-types'
9+
import type { ReactNode } from 'react'
10+
11+
export type ReactParamsObject<Value extends LocaleValue> = Record<
12+
Params<Value>[number],
13+
LocaleValue | ReactNode
14+
>
1015

1116
export type TranslateFn<Locale extends BaseLocale> = <
1217
Key extends LocaleKeys<Locale, undefined>,
1318
Value extends LocaleValue = ScopedValue<Locale, undefined, Key>,
1419
>(
1520
key: Key,
16-
...params: Params<Value>['length'] extends 0 ? [] : [ParamsObject<Value>]
21+
...params: Params<Value>['length'] extends 0 ? [] : [ReactParamsObject<Value>]
1722
) => string
1823

1924
export type ScopedTranslateFn<Locale extends BaseLocale> = <
@@ -25,5 +30,5 @@ export type ScopedTranslateFn<Locale extends BaseLocale> = <
2530
Value extends LocaleValue = ScopedValue<Locale, Scope, Key>,
2631
>(
2732
key: Key,
28-
...params: Params<Value>['length'] extends 0 ? [] : [ParamsObject<Value>]
33+
...params: Params<Value>['length'] extends 0 ? [] : [ReactParamsObject<Value>]
2934
) => string

tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@
1313
"jsx": "preserve"
1414
},
1515
"include": ["**/*.ts", "**/*.js", "**/.*.js", "**/*.tsx"],
16-
"exclude": ["**/dist/*.ts", "**/__typetests__/*.test.ts"]
16+
"exclude": [
17+
"**/dist/*.ts",
18+
"**/__typetests__/*.test.ts",
19+
"**/__typetests__/*.test.tsx"
20+
]
1721
}

0 commit comments

Comments
 (0)