From dee0118bcd862322422e95aa3ca03251ba934e0e Mon Sep 17 00:00:00 2001 From: Bobbie Goede Date: Mon, 27 Jan 2025 17:13:22 +0100 Subject: [PATCH] feat: configurate custom property `$i18n` type --- packages/vue-i18n-core/src/index.ts | 7 ++++- packages/vue-i18n-core/src/types.ts | 44 +++++++++++++++++++++++++++++ packages/vue-i18n/src/index.ts | 1 + packages/vue-i18n/src/vue.d.ts | 5 ++-- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/packages/vue-i18n-core/src/index.ts b/packages/vue-i18n-core/src/index.ts index e4cfeb26d..18940c5c4 100644 --- a/packages/vue-i18n-core/src/index.ts +++ b/packages/vue-i18n-core/src/index.ts @@ -99,7 +99,12 @@ export type { } from './legacy' export { initDev, initFeatureFlags, VERSION } from './misc' export type { I18nPluginOptions } from './plugin' -export type { Disposer } from './types' +export type { + Disposer, + GeneratedInstanceType, + GeneratedTypeConfig, + VueI18nInstance +} from './types' export type { IsEmptyObject, diff --git a/packages/vue-i18n-core/src/types.ts b/packages/vue-i18n-core/src/types.ts index 7bb1de228..b83ab54bb 100644 --- a/packages/vue-i18n-core/src/types.ts +++ b/packages/vue-i18n-core/src/types.ts @@ -1 +1,45 @@ +import type { IsNever } from '@intlify/core-base' +import type { ExportedGlobalComposer } from './i18n' +import type { VueI18n } from './legacy' + export type Disposer = () => void + +/** + * + * The interface used for narrowing types using generated types. + * + * @remarks + * + * The type generated by 3rd party (e.g. nuxt/i18n) + * + * @example + * ```ts + * // generated-i18n-types.d.ts (`.d.ts` file at your app) + * + * declare module '@intlify/vue-i18n-core' { + * interface GeneratedTypeConfig { + * legacy: false + * } + * } + * ``` + */ + +export interface GeneratedTypeConfig {} + +/** + * Narrowed i18n instance type based on `GeneratedTypeConfig['legacy']` + * + * - `never` (unset) resolves to `VueI18n | ExportedGlobalComposer` + * - `true` resolves to `VueI18n` + * - `false` resolves to `ExportedGlobalComposer` + */ +export type GeneratedInstanceType = + GeneratedTypeConfig extends Record<'legacy', infer Legacy> ? Legacy : never + +/** @VueI18nGeneral */ +export type VueI18nInstance = + IsNever extends true + ? VueI18n | ExportedGlobalComposer + : GeneratedInstanceType extends true + ? VueI18n + : ExportedGlobalComposer diff --git a/packages/vue-i18n/src/index.ts b/packages/vue-i18n/src/index.ts index f690fb6e1..dca19b3d1 100644 --- a/packages/vue-i18n/src/index.ts +++ b/packages/vue-i18n/src/index.ts @@ -110,6 +110,7 @@ export type { VueI18n, VueI18nDateTimeFormatting, VueI18nExtender, + VueI18nInstance, VueI18nNumberFormatting, VueI18nOptions, VueI18nResolveLocaleMessageTranslation, diff --git a/packages/vue-i18n/src/vue.d.ts b/packages/vue-i18n/src/vue.d.ts index 5d0986d9d..970110936 100644 --- a/packages/vue-i18n/src/vue.d.ts +++ b/packages/vue-i18n/src/vue.d.ts @@ -16,13 +16,12 @@ import type { DateTimeFormatResult, DefineDateTimeFormat, DefineLocaleMessage, - ExportedGlobalComposer, NumberFormat, NumberFormatResult, RemovedIndexResources, TranslateResult, Translation, - VueI18n, + VueI18nInstance, VueI18nOptions, VueMessageType } from '@intlify/vue-i18n-core' @@ -72,7 +71,7 @@ declare module 'vue' { * The locales, locale messages, and other resources managed by the instance referenced by this property are valid as global scope. * If the `i18n` component custom option is not specified, it's the same as the VueI18n instance that can be referenced by the i18n instance {@link I18n.global | global} property. */ - $i18n: VueI18n | ExportedGlobalComposer + $i18n: VueI18nInstance /** * Locale message translation *