From 60a2c495d11fd2d93ecdc718a707b9643f5791cf Mon Sep 17 00:00:00 2001 From: Pavel Kuzmin Date: Sat, 17 Aug 2024 14:42:04 +0500 Subject: [PATCH] add: types --- playground/nuxt.config.ts | 2 +- playground/package.json | 2 +- src/module.ts | 54 +-------------------------------------- src/runtime/01.plugin.ts | 32 +++++++++++++++++++++++ 4 files changed, 35 insertions(+), 55 deletions(-) diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 08e05be1..a02a6fd2 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -5,7 +5,7 @@ import { startSubprocess } from '@nuxt/devtools-kit' export default defineNuxtConfig({ modules: [ - '../src/module', + 'nuxt-i18n-micro', defineNuxtModule({ setup(_, nuxt) { if (!nuxt.options.dev) diff --git a/playground/package.json b/playground/package.json index 6bf7b9fa..b7980ae4 100644 --- a/playground/package.json +++ b/playground/package.json @@ -10,6 +10,6 @@ }, "dependencies": { "nuxt": "^3.12.4", - "nuxt-i18n-micro": "^1.1.1" + "nuxt-i18n-micro": "^1.1.2" } } diff --git a/src/module.ts b/src/module.ts index 32d61cb0..6b39d15a 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,6 +1,6 @@ import path from 'node:path' import { existsSync, mkdirSync, writeFileSync } from 'node:fs' -import { addPlugin, addServerHandler, addTypeTemplate, createResolver, defineNuxtModule, extendPages } from '@nuxt/kit' +import { addPlugin, addServerHandler, createResolver, defineNuxtModule, extendPages } from '@nuxt/kit' import type { HookResult } from '@nuxt/schema' import { setupDevToolsUI } from './devtools' @@ -186,58 +186,6 @@ export default defineNuxtModule({ // additionalRoutes.forEach(route => routesSet.add(route)) // }) - addTypeTemplate({ - filename: 'types/i18n.d.ts', - getContents() { - return ` - interface PluralTranslations { - singular: string; - plural: string; - } - - interface Translations { - [key: string]: string | number | boolean | Translations | PluralTranslations | unknown[] | null; - } - - declare module '#app' { - interface NuxtApp { - $getLocale: () => string; - $getLocales: () => string[]; - $t: >( - key: string, - params?: T, - defaultValue?: string - ) => string | number | boolean | Translations | PluralTranslations | unknown[] | unknown | null; - $tc: (key: string, count: number, defaultValue?: string) => string; - $mergeTranslations: (newTranslations: Translations) => void; - $switchLocale: (locale: string) => void; - $localeRoute: (to: RouteLocationRaw, locale?: string) => RouteLocationRaw; - $loadPageTranslations: (locale: string, routeName: string) => Promise; - } - } - - declare module 'vue/types/vue' { - interface Vue { - $getLocale: () => string; - $getLocales: () => string[]; - $t: >( - key: string, - params?: T, - defaultValue?: string - ) => string | number | boolean | Translations | PluralTranslations | unknown[] | unknown | null; - $tc: (key: string, count: number, defaultValue?: string) => string; - $mergeTranslations: (newTranslations: Translations) => void; - $switchLocale: (locale: string) => void; - $localeRoute: (to: RouteLocationRaw, locale?: string) => RouteLocationRaw; - $loadPageTranslations: (locale: string, routeName: string) => Promise; - } - } - - export {}; - ` - }, - }) - // Setup DevTools integration if (nuxt.options.dev) setupDevToolsUI(options, resolver.resolve) diff --git a/src/runtime/01.plugin.ts b/src/runtime/01.plugin.ts index 508987ee..f9edcb83 100644 --- a/src/runtime/01.plugin.ts +++ b/src/runtime/01.plugin.ts @@ -240,3 +240,35 @@ export default defineNuxtPlugin(async (_nuxtApp) => { }, } }) + +interface PluginsInjections { + $getLocale: () => string; + $getLocales: () => string[]; + $t: >( + key: string, + params?: T, + defaultValue?: string + ) => string | number | boolean | Translations | PluralTranslations | unknown[] | unknown | null; + $tc: (key: string, count: number, defaultValue?: string) => string; + $mergeTranslations: (newTranslations: Translations) => void; + $switchLocale: (locale: string) => void; + $localeRoute: (to: RouteLocationRaw, locale?: string) => RouteLocationRaw; + $loadPageTranslations: (locale: string, routeName: string) => Promise; +} + +declare module '#app' { + // eslint-disable-next-line @typescript-eslint/no-empty-object-type + interface NuxtApp extends PluginsInjections {} +} + +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-expect-error +declare module 'nuxt/dist/app/nuxt' { + // eslint-disable-next-line @typescript-eslint/no-empty-object-type + interface NuxtApp extends PluginsInjections {} +} + +declare module '@vue/runtime-core' { + // eslint-disable-next-line @typescript-eslint/no-empty-object-type + interface ComponentCustomProperties extends PluginsInjections {} +}