Skip to content

Commit

Permalink
Update next-translate.d.ts with new TranslateFunction interface
Browse files Browse the repository at this point in the history
  • Loading branch information
hichemfantar committed Jan 25, 2024
1 parent ef7efe0 commit cd74d89
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions examples/with-app-directory/next-translate.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import type { Paths, I18n, Translate } from 'next-translate'

type Tail<T> = T extends [unknown, ...infer Rest] ? Rest : never;

export interface TranslationsKeys {
// Example with "common" and "home" namespaces in "en" (the default language):
common: Paths<typeof import('./locales/en/common.json')>
home: Paths<typeof import('./locales/en/home.json')>
// Specify here all the namespaces you have...
}

export interface TranslateFunction<Namespace extends keyof TranslationsKeys> {
(
key: TranslationsKeys[Namespace],
...rest: Tail<Parameters<Translate>>
): string
<T extends string>(template: TemplateStringsArray): string
};

export interface TypeSafeTranslate<Namespace extends keyof TranslationsKeys>
extends Omit<I18n, 't'> {
t: {
(
key: TranslationsKeys[Namespace],
...rest: Tail<Parameters<Translate>>
): string
<T extends string>(template: TemplateStringsArray): string
}
t: TranslateFunction<Namespace>
}

declare module 'next-translate/useTranslation' {
export default function useTranslation<
Namespace extends keyof TranslationsKeys
>(namespace: Namespace): TypeSafeTranslate<Namespace>
}

declare module 'next-translate/getT' {
export default function getT<
Namespace extends keyof TranslationsKeys
>(locale?: string, namespace: Namespace): Promise<TranslateFunction<Namespace>>
}

0 comments on commit cd74d89

Please sign in to comment.