From c22de5c35b4781322c9ee17ad375ec0af2fe60a7 Mon Sep 17 00:00:00 2001 From: vben Date: Mon, 11 Jan 2021 21:05:16 +0800 Subject: [PATCH] fix: useI18n type --- src/hooks/web/useI18n.ts | 21 ++++++++++++++++++--- src/views/demo/feat/img-preview/index.vue | 5 +++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/hooks/web/useI18n.ts b/src/hooks/web/useI18n.ts index d0357950..334208b9 100644 --- a/src/hooks/web/useI18n.ts +++ b/src/hooks/web/useI18n.ts @@ -1,6 +1,21 @@ import { i18n } from '/@/locales/setupI18n'; -export function useI18n(namespace?: string) { +type I18nGlobalTranslation = { + (key: string): string; + (key: string, locale: string): string; + (key: string, locale: string, list: unknown[]): string; + (key: string, locale: string, named: Record): string; + (key: string, list: unknown[]): string; + (key: string, named: Record): string; +}; + +type I18nTranslationRestParameters = [string, any]; + +export function useI18n( + namespace?: string +): { + t: I18nGlobalTranslation; +} { function getKey(key: string) { if (!namespace) { return key; @@ -22,9 +37,9 @@ export function useI18n(namespace?: string) { const { t, ...methods } = i18n.global; - const tFn: typeof t = (key: string, ...arg: any) => { + const tFn: I18nGlobalTranslation = (key: string, ...arg: any[]) => { if (!key) return ''; - return t(getKey(key), ...(arg as Parameters)); + return t(getKey(key), ...(arg as I18nTranslationRestParameters)); }; return { ...methods, diff --git a/src/views/demo/feat/img-preview/index.vue b/src/views/demo/feat/img-preview/index.vue index ca462110..9e8b7a17 100644 --- a/src/views/demo/feat/img-preview/index.vue +++ b/src/views/demo/feat/img-preview/index.vue @@ -1,5 +1,6 @@