Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support generated locale type #1890

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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