Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/use-i18n/src/__tests__/usei18n.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,12 @@ describe('i18n hook', () => {
})

it('should work with a component', async () => {
const { result } = renderHook(() => useTranslation([]), {
wrapper: wrapper({ defaultLocale: 'en' }),
})
const { result } = renderHook(
() => useTranslation<{ 'with.identifier': 'Hello {identifier}' }>([]),
{
wrapper: wrapper({ defaultLocale: 'en' }),
},
)
const CustomComponent = ({ children }: { children: ReactNode }) => (
<p style={{ fontWeight: 'bold' }}>{children}</p>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@ expectType<string>(
)
expectError(t('describe.john', {}))
expectError(t('describe.john'))

// With react components as param value
expectType<string>(
t('doe.child', {
name: (
<p>
My name is<b>John</b>
</p>
),
}),
)
11 changes: 8 additions & 3 deletions packages/use-i18n/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ import type {
LocaleKeys,
LocaleValue,
Params,
ParamsObject,
ScopedValue,
Scopes,
} from 'international-types'
import type { ReactNode } from 'react'

export type ReactParamsObject<Value extends LocaleValue> = Record<
Params<Value>[number],
LocaleValue | ReactNode
>

export type TranslateFn<Locale extends BaseLocale> = <
Key extends LocaleKeys<Locale, undefined>,
Value extends LocaleValue = ScopedValue<Locale, undefined, Key>,
>(
key: Key,
...params: Params<Value>['length'] extends 0 ? [] : [ParamsObject<Value>]
...params: Params<Value>['length'] extends 0 ? [] : [ReactParamsObject<Value>]
) => string

export type ScopedTranslateFn<Locale extends BaseLocale> = <
Expand All @@ -25,5 +30,5 @@ export type ScopedTranslateFn<Locale extends BaseLocale> = <
Value extends LocaleValue = ScopedValue<Locale, Scope, Key>,
>(
key: Key,
...params: Params<Value>['length'] extends 0 ? [] : [ParamsObject<Value>]
...params: Params<Value>['length'] extends 0 ? [] : [ReactParamsObject<Value>]
) => string
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
"jsx": "preserve"
},
"include": ["**/*.ts", "**/*.js", "**/.*.js", "**/*.tsx"],
"exclude": ["**/dist/*.ts", "**/__typetests__/*.test.ts"]
"exclude": [
"**/dist/*.ts",
"**/__typetests__/*.test.ts",
"**/__typetests__/*.test.tsx"
]
}