-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Paths type for type safety (#1108)
* feat: add Paths type for type safety * fix to satisfy the constraint 'string'
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Paths, I18n, Translate } from 'next-translate' | ||
|
||
export interface TranslationsKeys { | ||
common: Paths<typeof import('./locales/en/common.json')> | ||
home: Paths<typeof import('./locales/en/home.json')> | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
declare module 'next-translate/useTranslation' { | ||
export default function useTranslation< | ||
Namespace extends keyof TranslationsKeys | ||
>(namespace: Namespace): TypeSafeTranslate<Namespace> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters