diff --git a/packages/core-base/src/runtime.ts b/packages/core-base/src/runtime.ts index 7ee29c17f..f80666538 100644 --- a/packages/core-base/src/runtime.ts +++ b/packages/core-base/src/runtime.ts @@ -11,6 +11,7 @@ import { } from '@intlify/shared' import { HelperNameMap } from '@intlify/message-compiler' import { Path } from './resolver' +import { IsNever } from './types' type ExtractToStringKey = Extract type ExtractToStringFunction = T[ExtractToStringKey] @@ -21,8 +22,38 @@ type StringConvertable = ExtractToStringKey extends never ? T : unknown +/** + * + * 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/core' { + * interface GeneratedTypeConfig { + * locale: "en" | "ja" + * } + * } + * ``` + */ +export interface GeneratedTypeConfig {} + +/** + * Generated locale which resolves to `never` if left unset + */ +export type GeneratedLocale = + GeneratedTypeConfig extends Record<'locale', infer CustomLocale> + ? CustomLocale + : never + /** @VueI18nGeneral */ -export type Locale = string +export type Locale = + IsNever extends true ? string : GeneratedLocale /** @VueI18nGeneral */ // prettier-ignore diff --git a/packages/petite-vue-i18n/src/index.ts b/packages/petite-vue-i18n/src/index.ts index 7002ce033..0b01eb2f8 100644 --- a/packages/petite-vue-i18n/src/index.ts +++ b/packages/petite-vue-i18n/src/index.ts @@ -42,7 +42,8 @@ export { MessageCompilerContext, CompileError, MessageContext, - RemovedIndexResources + RemovedIndexResources, + GeneratedTypeConfig } from '@intlify/core-base' export { VueMessageType, diff --git a/packages/vue-i18n-core/src/composer.ts b/packages/vue-i18n-core/src/composer.ts index cc379456b..b5bc124e3 100644 --- a/packages/vue-i18n-core/src/composer.ts +++ b/packages/vue-i18n-core/src/composer.ts @@ -109,7 +109,8 @@ import type { IsEmptyObject, CoreMissingType, JsonPaths, - TranslationsPaths + TranslationsPaths, + GeneratedLocale } from '@intlify/core-base' import type { VueDevToolsEmitter } from '@intlify/devtools-types' @@ -1274,11 +1275,13 @@ export interface Composer< | PickupLocales> | PickupLocales> | PickupLocales>, - Locales = OptionLocale extends Locale - ? IsNever extends true - ? Locale - : ResourceLocales - : OptionLocale | ResourceLocales + Locales = Locale extends GeneratedLocale + ? GeneratedLocale + : OptionLocale extends Locale + ? IsNever extends true + ? Locale + : ResourceLocales + : OptionLocale | ResourceLocales > extends ComposerCustom { /** * @remarks diff --git a/packages/vue-i18n-core/src/legacy.ts b/packages/vue-i18n-core/src/legacy.ts index 670071129..ce33eb622 100644 --- a/packages/vue-i18n-core/src/legacy.ts +++ b/packages/vue-i18n-core/src/legacy.ts @@ -46,7 +46,8 @@ import type { PickupPaths, PickupFormatPathKeys, IsEmptyObject, - IsNever + IsNever, + GeneratedLocale } from '@intlify/core-base' import type { VueDevToolsEmitter } from '@intlify/devtools-types' import type { @@ -1047,11 +1048,13 @@ export interface VueI18n< | PickupLocales> | PickupLocales> | PickupLocales>, - Locales = OptionLocale extends string - ? [ResourceLocales] extends [never] - ? Locale - : ResourceLocales - : OptionLocale | ResourceLocales, + Locales = Locale extends GeneratedLocale + ? GeneratedLocale + : OptionLocale extends string + ? [ResourceLocales] extends [never] + ? Locale + : ResourceLocales + : OptionLocale | ResourceLocales, Composition extends Composer< Messages, DateTimeFormats, diff --git a/packages/vue-i18n/src/index.ts b/packages/vue-i18n/src/index.ts index ae725bb8e..53c16ee0c 100644 --- a/packages/vue-i18n/src/index.ts +++ b/packages/vue-i18n/src/index.ts @@ -52,7 +52,8 @@ export { MessageCompilerContext, CompileError, MessageContext, - RemovedIndexResources + RemovedIndexResources, + GeneratedTypeConfig } from '@intlify/core-base' export { VueMessageType,