Skip to content

Commit

Permalink
feat: configurable custom property $i18n type (#2094)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored Jan 31, 2025
1 parent a9a8029 commit de2523d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
7 changes: 6 additions & 1 deletion packages/vue-i18n-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ export {
} from './legacy'
export { VERSION } from './misc'
export { I18nPluginOptions } from './plugin'
export { Disposer } from './types'
export {
Disposer,
GeneratedInstanceType,
GeneratedTypeConfig,
VueI18nInstance
} from './types'

export type {
IsEmptyObject,
Expand Down
43 changes: 43 additions & 0 deletions packages/vue-i18n-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
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<GeneratedInstanceType> extends true
? VueI18n | ExportedGlobalComposer
: GeneratedInstanceType extends true
? VueI18n
: ExportedGlobalComposer
2 changes: 1 addition & 1 deletion packages/vue-i18n/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export {
} from '../../vue-i18n-core/src/legacy'
export { I18nPluginOptions } from '../../vue-i18n-core/src/plugin'
export { VERSION } from './../../vue-i18n-core/src/misc'
export { Disposer } from './../../vue-i18n-core/src/types'
export { Disposer, VueI18nInstance } from './../../vue-i18n-core/src/types'

export type {
IsEmptyObject,
Expand Down
5 changes: 2 additions & 3 deletions packages/vue-i18n/src/vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ import type {
DefineDateTimeFormat,
DefineLocaleMessage,
RemovedIndexResources,
VueI18nInstance,
VueMessageType
} from '../../vue-i18n-core/src/composer'
import type { ExportedGlobalComposer } from '../../vue-i18n-core/src/i18n'
import type {
DateTimeFormatResult,
NumberFormatResult,
TranslateResult,
VueI18n,
VueI18nOptions
} from '../../vue-i18n-core/src/legacy'

Expand Down Expand Up @@ -74,7 +73,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
*
Expand Down

0 comments on commit de2523d

Please sign in to comment.