Skip to content

Commit de2523d

Browse files
authored
feat: configurable custom property $i18n type (#2094)
1 parent a9a8029 commit de2523d

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

packages/vue-i18n-core/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ export {
106106
} from './legacy'
107107
export { VERSION } from './misc'
108108
export { I18nPluginOptions } from './plugin'
109-
export { Disposer } from './types'
109+
export {
110+
Disposer,
111+
GeneratedInstanceType,
112+
GeneratedTypeConfig,
113+
VueI18nInstance
114+
} from './types'
110115

111116
export type {
112117
IsEmptyObject,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
1+
import type { IsNever } from '@intlify/core-base'
2+
import type { ExportedGlobalComposer } from './i18n'
3+
import type { VueI18n } from './legacy'
4+
15
export type Disposer = () => void
6+
7+
/**
8+
*
9+
* The interface used for narrowing types using generated types.
10+
*
11+
* @remarks
12+
*
13+
* The type generated by 3rd party (e.g. nuxt/i18n)
14+
*
15+
* @example
16+
* ```ts
17+
* // generated-i18n-types.d.ts (`.d.ts` file at your app)
18+
*
19+
* declare module '@intlify/vue-i18n-core' {
20+
* interface GeneratedTypeConfig {
21+
* legacy: false
22+
* }
23+
* }
24+
* ```
25+
*/
26+
export interface GeneratedTypeConfig {}
27+
28+
/**
29+
* Narrowed i18n instance type based on `GeneratedTypeConfig['legacy']`
30+
*
31+
* - `never` (unset) resolves to `VueI18n | ExportedGlobalComposer`
32+
* - `true` resolves to `VueI18n`
33+
* - `false` resolves to `ExportedGlobalComposer`
34+
*/
35+
export type GeneratedInstanceType =
36+
GeneratedTypeConfig extends Record<'legacy', infer Legacy> ? Legacy : never
37+
38+
/** @VueI18nGeneral */
39+
export type VueI18nInstance =
40+
IsNever<GeneratedInstanceType> extends true
41+
? VueI18n | ExportedGlobalComposer
42+
: GeneratedInstanceType extends true
43+
? VueI18n
44+
: ExportedGlobalComposer

packages/vue-i18n/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export {
125125
} from '../../vue-i18n-core/src/legacy'
126126
export { I18nPluginOptions } from '../../vue-i18n-core/src/plugin'
127127
export { VERSION } from './../../vue-i18n-core/src/misc'
128-
export { Disposer } from './../../vue-i18n-core/src/types'
128+
export { Disposer, VueI18nInstance } from './../../vue-i18n-core/src/types'
129129

130130
export type {
131131
IsEmptyObject,

packages/vue-i18n/src/vue.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ import type {
2121
DefineDateTimeFormat,
2222
DefineLocaleMessage,
2323
RemovedIndexResources,
24+
VueI18nInstance,
2425
VueMessageType
2526
} from '../../vue-i18n-core/src/composer'
26-
import type { ExportedGlobalComposer } from '../../vue-i18n-core/src/i18n'
2727
import type {
2828
DateTimeFormatResult,
2929
NumberFormatResult,
3030
TranslateResult,
31-
VueI18n,
3231
VueI18nOptions
3332
} from '../../vue-i18n-core/src/legacy'
3433

@@ -74,7 +73,7 @@ declare module 'vue' {
7473
* The locales, locale messages, and other resources managed by the instance referenced by this property are valid as global scope.
7574
* 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.
7675
*/
77-
$i18n: VueI18n | ExportedGlobalComposer
76+
$i18n: VueI18nInstance
7877
/**
7978
* Locale message translation
8079
*

0 commit comments

Comments
 (0)