Skip to content

Commit

Permalink
feat: support generated locale type (#1890)
Browse files Browse the repository at this point in the history
* feat: support generated locale type

* fix: resolve `GeneratedLocale` to `never` if unset

* fix: use `IntlifyGenratedTypeConfig` instead and resolve earlier

* fix: rename interface

* fix: export interface from `petite-vue-i18n`
  • Loading branch information
BobbieGoede authored Jul 16, 2024
1 parent e865cea commit 1643bb5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
33 changes: 32 additions & 1 deletion packages/core-base/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@intlify/shared'
import { HelperNameMap } from '@intlify/message-compiler'
import { Path } from './resolver'
import { IsNever } from './types'

type ExtractToStringKey<T> = Extract<keyof T, 'toString'>
type ExtractToStringFunction<T> = T[ExtractToStringKey<T>]
Expand All @@ -21,8 +22,38 @@ type StringConvertable<T> = ExtractToStringKey<T> extends never
? T
: unknown

/**
*
* The interface used for narrowing types using generated types.
*
* @remarks
*
* The type generated by 3rd party (e.g. nuxt/i18n)
*
* @example
* ```ts
* // generated-i18n-types.d.ts (`.d.ts` file at your app)
*
* declare module '@intlify/core' {
* interface GeneratedTypeConfig {
* locale: "en" | "ja"
* }
* }
* ```
*/
export interface GeneratedTypeConfig {}

/**
* Generated locale which resolves to `never` if left unset
*/
export type GeneratedLocale =
GeneratedTypeConfig extends Record<'locale', infer CustomLocale>
? CustomLocale
: never

/** @VueI18nGeneral */
export type Locale = string
export type Locale =
IsNever<GeneratedLocale> extends true ? string : GeneratedLocale

/** @VueI18nGeneral */
// prettier-ignore
Expand Down
3 changes: 2 additions & 1 deletion packages/petite-vue-i18n/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export {
MessageCompilerContext,
CompileError,
MessageContext,
RemovedIndexResources
RemovedIndexResources,
GeneratedTypeConfig
} from '@intlify/core-base'
export {
VueMessageType,
Expand Down
15 changes: 9 additions & 6 deletions packages/vue-i18n-core/src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ import type {
IsEmptyObject,
CoreMissingType,
JsonPaths,
TranslationsPaths
TranslationsPaths,
GeneratedLocale
} from '@intlify/core-base'
import type { VueDevToolsEmitter } from '@intlify/devtools-types'

Expand Down Expand Up @@ -1274,11 +1275,13 @@ export interface Composer<
| PickupLocales<NonNullable<Messages>>
| PickupLocales<NonNullable<DateTimeFormats>>
| PickupLocales<NonNullable<NumberFormats>>,
Locales = OptionLocale extends Locale
? IsNever<ResourceLocales> extends true
? Locale
: ResourceLocales
: OptionLocale | ResourceLocales
Locales = Locale extends GeneratedLocale
? GeneratedLocale
: OptionLocale extends Locale
? IsNever<ResourceLocales> extends true
? Locale
: ResourceLocales
: OptionLocale | ResourceLocales
> extends ComposerCustom {
/**
* @remarks
Expand Down
15 changes: 9 additions & 6 deletions packages/vue-i18n-core/src/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ import type {
PickupPaths,
PickupFormatPathKeys,
IsEmptyObject,
IsNever
IsNever,
GeneratedLocale
} from '@intlify/core-base'
import type { VueDevToolsEmitter } from '@intlify/devtools-types'
import type {
Expand Down Expand Up @@ -1047,11 +1048,13 @@ export interface VueI18n<
| PickupLocales<NonNullable<Messages>>
| PickupLocales<NonNullable<DateTimeFormats>>
| PickupLocales<NonNullable<NumberFormats>>,
Locales = OptionLocale extends string
? [ResourceLocales] extends [never]
? Locale
: ResourceLocales
: OptionLocale | ResourceLocales,
Locales = Locale extends GeneratedLocale
? GeneratedLocale
: OptionLocale extends string
? [ResourceLocales] extends [never]
? Locale
: ResourceLocales
: OptionLocale | ResourceLocales,
Composition extends Composer<
Messages,
DateTimeFormats,
Expand Down
3 changes: 2 additions & 1 deletion packages/vue-i18n/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export {
MessageCompilerContext,
CompileError,
MessageContext,
RemovedIndexResources
RemovedIndexResources,
GeneratedTypeConfig
} from '@intlify/core-base'
export {
VueMessageType,
Expand Down

0 comments on commit 1643bb5

Please sign in to comment.