Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type safety for getT function #1184

Merged
merged 4 commits into from
Jan 26, 2024
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
30 changes: 20 additions & 10 deletions docs/type-safety.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,32 @@ export interface TranslationsKeys {
// Specify here all the namespaces you have...
}

export interface TypeSafeTranslate<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 TranslationNamespace>
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 extends TranslationNamespace
>(namespace: Namespace): TypeSafeTranslate<Namespace>
}

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

Then type safety should work:
Expand All @@ -38,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>
32 changes: 23 additions & 9 deletions examples/with-app-directory/next-translate.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
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 TypeSafeTranslate<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 TranslationNamespace>
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 extends TranslationNamespace
>(namespace: Namespace): TypeSafeTranslate<Namespace>
}

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