Skip to content

Commit

Permalink
Refactor type safety in TranslateFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
hichemfantar committed Jan 25, 2024
1 parent 1c8cc27 commit eeef92e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions docs/type-safety.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,30 @@ export interface TranslationsKeys {
// Specify here all the namespaces you have...
}

export interface TranslateFunction<Namespace extends keyof TranslationsKeys> {
type TranslationNamespace = keyof TranslationsKeys;

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

export interface TypeSafeTranslate<Namespace extends keyof TranslationsKeys>
export interface TypeSafeTranslate<Namespace extends TranslationNamespace>
extends Omit<I18n, 't'> {
t: TranslateFunction<Namespace>
}

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

declare module 'next-translate/getT' {
export default function getT<
Namespace extends keyof TranslationsKeys
Namespace extends TranslationNamespace
>(locale?: string, namespace: Namespace): Promise<TranslateFunction<Namespace>>
}
```
Expand All @@ -46,4 +48,4 @@ Then type safety should work:
<img width="282" alt="Screenshot 2023-07-17 at 19 22 00" src="https://github.com/aralroca/next-translate/assets/13313058/616987b4-e49b-4cf2-b511-cdfaba57e1d2">
Reference: https://github.com/aralroca/next-translate/pull/1108
Reference: <https://github.com/aralroca/next-translate/pull/1108>

0 comments on commit eeef92e

Please sign in to comment.