Skip to content

Commit ddd07fb

Browse files
authored
fix: legacy and composition type narrowing for vue-i18n (#3526)
* chore(deps): update `vue-i18n` * fix: narrow `vue-i18n` types based on `types` option * fix: type issues
1 parent 04026fc commit ddd07fb

7 files changed

Lines changed: 81 additions & 94 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
},
8484
"dependencies": {
8585
"@intlify/h3": "^0.6.1",
86-
"@intlify/shared": "^11.1.2",
86+
"@intlify/shared": "^11.1.3",
8787
"@intlify/unplugin-vue-i18n": "^6.0.5",
8888
"@intlify/utils": "^0.13.0",
8989
"@miyaneee/rollup-plugin-json5": "^1.2.0",
@@ -105,7 +105,7 @@
105105
"ufo": "^1.5.4",
106106
"unplugin": "^2.2.2",
107107
"unplugin-vue-router": "^0.12.0",
108-
"vue-i18n": "^11.1.2",
108+
"vue-i18n": "^11.1.3",
109109
"vue-router": "^4.5.0"
110110
},
111111
"devDependencies": {

pnpm-lock.yaml

Lines changed: 41 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gen.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,12 @@ declare module 'vue-router' {
194194
}`
195195

196196
export function generateI18nTypes(nuxt: Nuxt, { userOptions: options, normalizedLocales }: I18nNuxtContext) {
197-
const vueI18nTypes = options.types === 'legacy' ? ['VueI18n'] : ['ExportedGlobalComposer', 'Composer']
197+
const legacyTypes = options.types === 'legacy'
198+
const i18nType = legacyTypes ? 'VueI18n' : 'Composer'
198199
const generatedLocales = simplifyLocaleOptions(nuxt, options)
199200
const resolvedLocaleType = isString(generatedLocales) ? 'Locale[]' : 'LocaleObject[]'
200201
const narrowedLocaleType = normalizedLocales.map(x => JSON.stringify(x.code)).join(' | ') || 'string'
201202

202-
const i18nType = `${vueI18nTypes.join(' & ')} & NuxtI18nRoutingCustomProperties<${resolvedLocaleType}>`
203-
204203
const globalTranslationTypes = `
205204
declare global {
206205
var $t: (${i18nType})['t']
@@ -213,30 +212,30 @@ declare global {
213212

214213
// prettier-ignore
215214
return `// Generated by @nuxtjs/i18n
216-
import type { ${vueI18nTypes.join(', ')} } from 'vue-i18n'
217-
import type { NuxtI18nRoutingCustomProperties, ComposerCustomProperties } from '${relative(
215+
import type { ${i18nType} } from 'vue-i18n'
216+
import type { ComposerCustomProperties } from '${relative(
218217
join(nuxt.options.buildDir, 'types'),
219218
resolve(runtimeDir, 'types.ts')
220219
)}'
221220
import type { Strategies, Directions, LocaleObject } from '${relative(
222221
join(nuxt.options.buildDir, 'types'),
223-
resolve(distDir, 'types.d.ts')
222+
resolve(distDir, 'types.d.mts')
224223
)}'
225224
226225
declare module 'vue-i18n' {
227226
interface ComposerCustom extends ComposerCustomProperties<${resolvedLocaleType}> {}
228-
interface ExportedGlobalComposer extends NuxtI18nRoutingCustomProperties<${resolvedLocaleType}> {}
229-
interface VueI18n extends NuxtI18nRoutingCustomProperties<${resolvedLocaleType}> {}
227+
interface ExportedGlobalComposer extends ComposerCustomProperties<${resolvedLocaleType}> {}
228+
interface VueI18n extends ComposerCustomProperties<${resolvedLocaleType}> {}
230229
}
231230
232231
declare module '@intlify/core-base' {
233232
// generated based on configured locales
234233
interface GeneratedTypeConfig {
235234
locale: ${narrowedLocaleType}
235+
legacy: ${legacyTypes}
236236
}
237237
}
238238
239-
240239
declare module '#app' {
241240
interface NuxtApp {
242241
$i18n: ${i18nType}

src/internal-global-types.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type { Composer, ExportedGlobalComposer, VueI18n } from 'vue-i18n'
2-
import type { ComposerCustomProperties, NuxtI18nRoutingCustomProperties } from './runtime/types'
1+
import type { Composer, VueI18n } from 'vue-i18n'
2+
import type { ComposerCustomProperties } from './runtime/types'
33

44
declare module 'vue-i18n' {
55
interface ComposerCustom extends ComposerCustomProperties {}
6-
interface ExportedGlobalComposer extends NuxtI18nRoutingCustomProperties {}
7-
interface VueI18n extends NuxtI18nRoutingCustomProperties {}
6+
interface ExportedGlobalComposer extends ComposerCustomProperties {}
7+
interface VueI18n extends ComposerCustomProperties {}
88
}
99

1010
declare module '#app' {
1111
interface NuxtApp {
12-
$i18n: VueI18n & ExportedGlobalComposer & Composer & NuxtI18nRoutingCustomProperties
12+
$i18n: VueI18n & Composer
1313
}
1414
}
1515

src/runtime/plugins/i18n.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export default defineNuxtPlugin({
9090
extendI18n(i18n, {
9191
extendComposer(composer) {
9292
const _locales = ref<Locale[] | LocaleObject[]>(runtimeI18n.locales)
93-
composer.locales = computed(() => _locales.value as unknown as typeof composer.locales.value)
93+
composer.locales = computed(() => _locales.value)
9494

9595
const _localeCodes = ref<Locale[]>(localeCodes)
9696
composer.localeCodes = computed(() => _localeCodes.value)

src/runtime/routing/head.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ type HeadContext = {
3535
function createHeadContext(options: Required<I18nHeadOptions>): HeadContext {
3636
const nuxtApp = useNuxtApp()
3737
const locale = unref(nuxtApp.$i18n.locale)
38-
const locales = unref(nuxtApp.$i18n.locales).map(x => (isString(x) ? { code: x } : x))
39-
const currentLocale: LocaleObject = locales.find(l => l.code === locale) || { code: locale }
38+
const locales = unref(nuxtApp.$i18n.locales).map(x => (isString(x) ? { code: x } : (x as LocaleObject)))
39+
const currentLocale = locales.find(l => l.code === locale) || { code: locale }
4040
const baseUrl = joinURL(unref(getComposer(nuxtApp.$i18n).baseUrl), nuxtApp.$config.app.baseURL)
4141
const runtimeI18n = nuxtApp.$config.public.i18n as I18nPublicRuntimeConfig
4242

src/runtime/types.ts

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,28 @@ type BeforeLanguageSwitchHandler = (
4343
*/
4444
type LanguageSwitchedHandler = (oldLocale: Locale, newLocale: Locale) => Promise<void>
4545

46-
interface SharedProperties {
46+
/**
47+
* @template ConfiguredLocaleType - The type of the locales configuration. Can be an array of string codes or an array of {@link LocaleObject}.
48+
*/
49+
export interface ComposerCustomProperties<
50+
ConfiguredLocaleType extends Locale[] | LocaleObject[] = Locale[] | LocaleObject[]
51+
> {
52+
/**
53+
* List of locales - can either be an array of string codes (e.g. `['en', 'fr']`) or an array of {@link LocaleObject} for more complex configurations
54+
*/
55+
locales: ComputedRef<ConfiguredLocaleType>
56+
/**
57+
* List of locale codes
58+
*/
59+
localeCodes: ComputedRef<Locale[]>
60+
/**
61+
* Base URL that is used in generating canonical links
62+
*/
63+
baseUrl: ComputedRef<string>
64+
/**
65+
* Current locale properties.
66+
*/
67+
localeProperties: ComputedRef<LocaleObject>
4768
/**
4869
* Routing strategy.
4970
*/
@@ -131,54 +152,6 @@ interface SharedProperties {
131152
__extendComposer: (instance: Composer | VueI18n | ExportedGlobalComposer) => void
132153
}
133154

134-
export interface ComposerCustomProperties<
135-
ConfiguredLocaleType extends Locale[] | LocaleObject[] = Locale[] | LocaleObject[]
136-
> extends SharedProperties {
137-
/**
138-
* List of locales
139-
*
140-
* @remarks
141-
* Can either be an array of string codes (e.g. `['en', 'fr']`) or an array of {@link LocaleObject} for more complex configurations
142-
*/
143-
locales: ComputedRef<ConfiguredLocaleType>
144-
/**
145-
* List of locale codes
146-
*/
147-
localeCodes: ComputedRef<Locale[]>
148-
/**
149-
* Base URL that is used in generating canonical links
150-
*/
151-
baseUrl: ComputedRef<string>
152-
/**
153-
* Current locale properties.
154-
*/
155-
localeProperties: ComputedRef<LocaleObject>
156-
}
157-
158-
export interface NuxtI18nRoutingCustomProperties<
159-
ConfiguredLocaleType extends Locale[] | LocaleObject[] = Locale[] | LocaleObject[]
160-
> extends SharedProperties {
161-
/**
162-
* List of locales
163-
*
164-
* @remarks
165-
* Can either be an array of string codes (e.g. `['en', 'fr']`) or an array of {@link LocaleObject} for more complex configurations
166-
*/
167-
readonly locales: ConfiguredLocaleType
168-
/**
169-
* List of locale codes
170-
*/
171-
readonly localeCodes: Locale[]
172-
/**
173-
* Base URL that is used in generating canonical links
174-
*/
175-
baseUrl: string
176-
/**
177-
* Current locale properties.
178-
*/
179-
localeProperties: LocaleObject
180-
}
181-
182155
declare module '#app' {
183156
interface NuxtApp {
184157
/** @internal */

0 commit comments

Comments
 (0)