From ef7efe0ae930e420c3ccfb294a4347706ea95d19 Mon Sep 17 00:00:00 2001 From: Hichem Fantar Date: Thu, 25 Jan 2024 16:39:46 +0100 Subject: [PATCH 1/4] Update type-safety.md --- docs/type-safety.md | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/docs/type-safety.md b/docs/type-safety.md index ea9daf0..64e5151 100644 --- a/docs/type-safety.md +++ b/docs/type-safety.md @@ -14,15 +14,17 @@ export interface TranslationsKeys { // Specify here all the namespaces you have... } +export interface TranslateFunction { + ( + key: TranslationsKeys[Namespace], + ...rest: Tail> + ): string + (template: TemplateStringsArray): string +}; + export interface TypeSafeTranslate extends Omit { - t: { - ( - key: TranslationsKeys[Namespace], - ...rest: Tail> - ): string - (template: TemplateStringsArray): string - } + t: TranslateFunction } declare module 'next-translate/useTranslation' { @@ -30,6 +32,12 @@ declare module 'next-translate/useTranslation' { Namespace extends keyof TranslationsKeys >(namespace: Namespace): TypeSafeTranslate } + +declare module 'next-translate/getT' { + export default function getT< + Namespace extends keyof TranslationsKeys + >(locale?: string, namespace: Namespace): Promise> +} ``` Then type safety should work: From cd74d898cfe355b9e9bdeb5ec05e0a23cf244575 Mon Sep 17 00:00:00 2001 From: Hichem Fantar Date: Thu, 25 Jan 2024 16:52:36 +0100 Subject: [PATCH 2/4] Update next-translate.d.ts with new TranslateFunction interface --- .../with-app-directory/next-translate.d.ts | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/examples/with-app-directory/next-translate.d.ts b/examples/with-app-directory/next-translate.d.ts index ea3fac6..870df3f 100644 --- a/examples/with-app-directory/next-translate.d.ts +++ b/examples/with-app-directory/next-translate.d.ts @@ -1,19 +1,25 @@ import type { Paths, I18n, Translate } from 'next-translate' +type Tail = T extends [unknown, ...infer Rest] ? Rest : never; + export interface TranslationsKeys { + // Example with "common" and "home" namespaces in "en" (the default language): common: Paths home: Paths + // Specify here all the namespaces you have... } +export interface TranslateFunction { + ( + key: TranslationsKeys[Namespace], + ...rest: Tail> + ): string + (template: TemplateStringsArray): string +}; + export interface TypeSafeTranslate extends Omit { - t: { - ( - key: TranslationsKeys[Namespace], - ...rest: Tail> - ): string - (template: TemplateStringsArray): string - } + t: TranslateFunction } declare module 'next-translate/useTranslation' { @@ -21,3 +27,9 @@ declare module 'next-translate/useTranslation' { Namespace extends keyof TranslationsKeys >(namespace: Namespace): TypeSafeTranslate } + +declare module 'next-translate/getT' { + export default function getT< + Namespace extends keyof TranslationsKeys + >(locale?: string, namespace: Namespace): Promise> +} From 1c8cc2717a5b752ae9973ae41533819e6fba99da Mon Sep 17 00:00:00 2001 From: Hichem Fantar Date: Thu, 25 Jan 2024 17:25:38 +0100 Subject: [PATCH 3/4] Refactor translation types in next-translate.d.ts --- examples/with-app-directory/next-translate.d.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/with-app-directory/next-translate.d.ts b/examples/with-app-directory/next-translate.d.ts index 870df3f..ed1185a 100644 --- a/examples/with-app-directory/next-translate.d.ts +++ b/examples/with-app-directory/next-translate.d.ts @@ -9,7 +9,9 @@ export interface TranslationsKeys { // Specify here all the namespaces you have... } -export interface TranslateFunction { +type TranslationNamespace = keyof TranslationsKeys; + +export interface TranslateFunction { ( key: TranslationsKeys[Namespace], ...rest: Tail> @@ -17,19 +19,19 @@ export interface TranslateFunction { (template: TemplateStringsArray): string }; -export interface TypeSafeTranslate +export interface TypeSafeTranslate extends Omit { t: TranslateFunction } declare module 'next-translate/useTranslation' { export default function useTranslation< - Namespace extends keyof TranslationsKeys + Namespace extends TranslationNamespace >(namespace: Namespace): TypeSafeTranslate } declare module 'next-translate/getT' { export default function getT< - Namespace extends keyof TranslationsKeys + Namespace extends TranslationNamespace >(locale?: string, namespace: Namespace): Promise> } From eeef92e3bbbe93fc36ead697255db66ed47c9563 Mon Sep 17 00:00:00 2001 From: Hichem Fantar Date: Thu, 25 Jan 2024 17:35:41 +0100 Subject: [PATCH 4/4] Refactor type safety in TranslateFunction --- docs/type-safety.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/type-safety.md b/docs/type-safety.md index 64e5151..43532dd 100644 --- a/docs/type-safety.md +++ b/docs/type-safety.md @@ -14,7 +14,9 @@ export interface TranslationsKeys { // Specify here all the namespaces you have... } -export interface TranslateFunction { +type TranslationNamespace = keyof TranslationsKeys; + +export interface TranslateFunction { ( key: TranslationsKeys[Namespace], ...rest: Tail> @@ -22,20 +24,20 @@ export interface TranslateFunction { (template: TemplateStringsArray): string }; -export interface TypeSafeTranslate +export interface TypeSafeTranslate extends Omit { t: TranslateFunction } declare module 'next-translate/useTranslation' { export default function useTranslation< - Namespace extends keyof TranslationsKeys + Namespace extends TranslationNamespace >(namespace: Namespace): TypeSafeTranslate } declare module 'next-translate/getT' { export default function getT< - Namespace extends keyof TranslationsKeys + Namespace extends TranslationNamespace >(locale?: string, namespace: Namespace): Promise> } ``` @@ -46,4 +48,4 @@ Then type safety should work: Screenshot 2023-07-17 at 19 22 00 -Reference: https://github.com/aralroca/next-translate/pull/1108 +Reference: