Skip to content

Commit 491ecd9

Browse files
authored
feat: configurate custom property $i18n type (#2093)
1 parent e27a54b commit 491ecd9

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ export type {
9999
} from './legacy'
100100
export { initDev, initFeatureFlags, VERSION } from './misc'
101101
export type { I18nPluginOptions } from './plugin'
102-
export type { Disposer } from './types'
102+
export type {
103+
Disposer,
104+
GeneratedInstanceType,
105+
GeneratedTypeConfig,
106+
VueI18nInstance
107+
} from './types'
103108

104109
export type {
105110
IsEmptyObject,

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,45 @@
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+
27+
export interface GeneratedTypeConfig {}
28+
29+
/**
30+
* Narrowed i18n instance type based on `GeneratedTypeConfig['legacy']`
31+
*
32+
* - `never` (unset) resolves to `VueI18n | ExportedGlobalComposer`
33+
* - `true` resolves to `VueI18n`
34+
* - `false` resolves to `ExportedGlobalComposer`
35+
*/
36+
export type GeneratedInstanceType =
37+
GeneratedTypeConfig extends Record<'legacy', infer Legacy> ? Legacy : never
38+
39+
/** @VueI18nGeneral */
40+
export type VueI18nInstance =
41+
IsNever<GeneratedInstanceType> extends true
42+
? VueI18n | ExportedGlobalComposer
43+
: GeneratedInstanceType extends true
44+
? VueI18n
45+
: ExportedGlobalComposer

packages/vue-i18n/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export type {
110110
VueI18n,
111111
VueI18nDateTimeFormatting,
112112
VueI18nExtender,
113+
VueI18nInstance,
113114
VueI18nNumberFormatting,
114115
VueI18nOptions,
115116
VueI18nResolveLocaleMessageTranslation,

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ import type {
1616
DateTimeFormatResult,
1717
DefineDateTimeFormat,
1818
DefineLocaleMessage,
19-
ExportedGlobalComposer,
2019
NumberFormat,
2120
NumberFormatResult,
2221
RemovedIndexResources,
2322
TranslateResult,
2423
Translation,
25-
VueI18n,
24+
VueI18nInstance,
2625
VueI18nOptions,
2726
VueMessageType
2827
} from '@intlify/vue-i18n-core'
@@ -72,7 +71,7 @@ declare module 'vue' {
7271
* The locales, locale messages, and other resources managed by the instance referenced by this property are valid as global scope.
7372
* 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.
7473
*/
75-
$i18n: VueI18n | ExportedGlobalComposer
74+
$i18n: VueI18nInstance
7675
/**
7776
* Locale message translation
7877
*

0 commit comments

Comments
 (0)