diff --git a/packages/use-i18n/src/__tests__/usei18n.tsx b/packages/use-i18n/src/__tests__/usei18n.tsx index c819eb948..af0bcbcb3 100644 --- a/packages/use-i18n/src/__tests__/usei18n.tsx +++ b/packages/use-i18n/src/__tests__/usei18n.tsx @@ -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 }) => (

{children}

) diff --git a/packages/use-i18n/src/__typetests__/t.test.ts b/packages/use-i18n/src/__typetests__/t.test.tsx similarity index 85% rename from packages/use-i18n/src/__typetests__/t.test.ts rename to packages/use-i18n/src/__typetests__/t.test.tsx index 5f5b755e5..3444dc568 100644 --- a/packages/use-i18n/src/__typetests__/t.test.ts +++ b/packages/use-i18n/src/__typetests__/t.test.tsx @@ -47,3 +47,14 @@ expectType( ) expectError(t('describe.john', {})) expectError(t('describe.john')) + +// With react components as param value +expectType( + t('doe.child', { + name: ( +

+ My name isJohn +

+ ), + }), +) diff --git a/packages/use-i18n/src/types.ts b/packages/use-i18n/src/types.ts index 579e0bc4f..2c6ea1bb6 100644 --- a/packages/use-i18n/src/types.ts +++ b/packages/use-i18n/src/types.ts @@ -3,17 +3,22 @@ import type { LocaleKeys, LocaleValue, Params, - ParamsObject, ScopedValue, Scopes, } from 'international-types' +import type { ReactNode } from 'react' + +export type ReactParamsObject = Record< + Params[number], + LocaleValue | ReactNode +> export type TranslateFn = < Key extends LocaleKeys, Value extends LocaleValue = ScopedValue, >( key: Key, - ...params: Params['length'] extends 0 ? [] : [ParamsObject] + ...params: Params['length'] extends 0 ? [] : [ReactParamsObject] ) => string export type ScopedTranslateFn = < @@ -25,5 +30,5 @@ export type ScopedTranslateFn = < Value extends LocaleValue = ScopedValue, >( key: Key, - ...params: Params['length'] extends 0 ? [] : [ParamsObject] + ...params: Params['length'] extends 0 ? [] : [ReactParamsObject] ) => string diff --git a/tsconfig.json b/tsconfig.json index dbdc8853d..21833cd8b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" + ] }