Skip to content

Commit

Permalink
feat(types): add NuxtI18nInstance type for use with custom accessors (#…
Browse files Browse the repository at this point in the history
…1147)

Fixes #1146
  • Loading branch information
rchl committed Apr 13, 2021
1 parent c4f7465 commit aa4f4d8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
19 changes: 17 additions & 2 deletions types/index.d.ts
@@ -1,7 +1,8 @@
import './vue'
import { Locale, I18nOptions } from 'vue-i18n'
import VueI18n, { Locale, I18nOptions, IVueI18n } from 'vue-i18n'
import { Context as NuxtContext } from '@nuxt/types'
import './vue'

// Deprecated
export { NuxtVueI18n } from './nuxt-i18n'

export { Locale }
Expand Down Expand Up @@ -71,3 +72,17 @@ export interface Options extends BaseOptions {
vueI18nLoader?: boolean
vuex?: VuexOptions | false
}

export interface IVueI18nNuxt extends Required<BaseOptions> {
finalizePendingLocaleChange(): Promise<void>
getBrowserLocale(): string | undefined
getLocaleCookie(): string | undefined
loadedLanguages: string[] | undefined
localeCodes: readonly Locale[]
localeProperties: LocaleObject
setLocale(locale: string): Promise<void>
setLocaleCookie(locale: string): void
waitForPendingLocaleChange(): Promise<void>
}

export type NuxtI18nInstance = VueI18n & IVueI18n
30 changes: 9 additions & 21 deletions types/vue.d.ts
@@ -1,10 +1,10 @@
import 'vue'
import 'vuex'
import 'vue-i18n'
import '@nuxt/types'
import { Location, RawLocation, Route } from 'vue-router'
import VueI18n, { IVueI18n } from 'vue-i18n'
import { MetaInfo } from 'vue-meta'
import { BaseOptions, LocaleObject, Options } from '.'
import { Options, NuxtI18nInstance, IVueI18nNuxt } from '.'

interface NuxtI18nComponentOptions {
paths?: {
Expand All @@ -29,22 +29,6 @@ interface NuxtI18nHeadOptions {

type NuxtI18nMeta = Required<Pick<MetaInfo, 'htmlAttrs' | 'link' | 'meta'>>

declare module 'vue-i18n' {
// the VueI18n class expands here: https://goo.gl/Xtp9EG
// it is necessary for the $i18n property in Vue interface: "readonly $i18n: VueI18n & IVueI18n"
interface IVueI18n extends Required<BaseOptions> {
finalizePendingLocaleChange(): Promise<void>
getBrowserLocale(): string | undefined
getLocaleCookie(): string | undefined
loadedLanguages: string[] | undefined
localeCodes: readonly Locale[]
localeProperties: LocaleObject
setLocale(locale: string): Promise<void>
setLocaleCookie(locale: string): void
waitForPendingLocaleChange(): Promise<void>
}
}

interface NuxtI18nApi {
getRouteBaseName(route?: Route): string | undefined
localePath(route: RawLocation, locale?: string): string
Expand All @@ -53,6 +37,10 @@ interface NuxtI18nApi {
switchLocalePath(locale: string): string
}

declare module 'vue-i18n' {
interface IVueI18n extends IVueI18nNuxt {}
}

declare module 'vue/types/vue' {
interface Vue extends NuxtI18nApi {
// $i18n is already added by vue-i18n.
Expand All @@ -70,11 +58,11 @@ declare module 'vue/types/options' {

declare module '@nuxt/types' {
interface Context extends NuxtI18nApi {
i18n: VueI18n & IVueI18n
i18n: NuxtI18nInstance
}

interface NuxtAppOptions extends NuxtI18nApi {
i18n: VueI18n & IVueI18n
i18n: NuxtI18nInstance
}

interface NuxtConfig {
Expand All @@ -84,6 +72,6 @@ declare module '@nuxt/types' {

declare module 'vuex/types/index' {
interface Store<S> extends NuxtI18nApi {
$i18n: VueI18n & IVueI18n
$i18n: NuxtI18nInstance
}
}

0 comments on commit aa4f4d8

Please sign in to comment.