Skip to content

Commit 1643bb5

Browse files
authored
feat: support generated locale type (#1890)
* 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`
1 parent e865cea commit 1643bb5

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

packages/core-base/src/runtime.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '@intlify/shared'
1212
import { HelperNameMap } from '@intlify/message-compiler'
1313
import { Path } from './resolver'
14+
import { IsNever } from './types'
1415

1516
type ExtractToStringKey<T> = Extract<keyof T, 'toString'>
1617
type ExtractToStringFunction<T> = T[ExtractToStringKey<T>]
@@ -21,8 +22,38 @@ type StringConvertable<T> = ExtractToStringKey<T> extends never
2122
? T
2223
: unknown
2324

25+
/**
26+
*
27+
* The interface used for narrowing types using generated types.
28+
*
29+
* @remarks
30+
*
31+
* The type generated by 3rd party (e.g. nuxt/i18n)
32+
*
33+
* @example
34+
* ```ts
35+
* // generated-i18n-types.d.ts (`.d.ts` file at your app)
36+
*
37+
* declare module '@intlify/core' {
38+
* interface GeneratedTypeConfig {
39+
* locale: "en" | "ja"
40+
* }
41+
* }
42+
* ```
43+
*/
44+
export interface GeneratedTypeConfig {}
45+
46+
/**
47+
* Generated locale which resolves to `never` if left unset
48+
*/
49+
export type GeneratedLocale =
50+
GeneratedTypeConfig extends Record<'locale', infer CustomLocale>
51+
? CustomLocale
52+
: never
53+
2454
/** @VueI18nGeneral */
25-
export type Locale = string
55+
export type Locale =
56+
IsNever<GeneratedLocale> extends true ? string : GeneratedLocale
2657

2758
/** @VueI18nGeneral */
2859
// prettier-ignore

packages/petite-vue-i18n/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export {
4242
MessageCompilerContext,
4343
CompileError,
4444
MessageContext,
45-
RemovedIndexResources
45+
RemovedIndexResources,
46+
GeneratedTypeConfig
4647
} from '@intlify/core-base'
4748
export {
4849
VueMessageType,

packages/vue-i18n-core/src/composer.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ import type {
109109
IsEmptyObject,
110110
CoreMissingType,
111111
JsonPaths,
112-
TranslationsPaths
112+
TranslationsPaths,
113+
GeneratedLocale
113114
} from '@intlify/core-base'
114115
import type { VueDevToolsEmitter } from '@intlify/devtools-types'
115116

@@ -1274,11 +1275,13 @@ export interface Composer<
12741275
| PickupLocales<NonNullable<Messages>>
12751276
| PickupLocales<NonNullable<DateTimeFormats>>
12761277
| PickupLocales<NonNullable<NumberFormats>>,
1277-
Locales = OptionLocale extends Locale
1278-
? IsNever<ResourceLocales> extends true
1279-
? Locale
1280-
: ResourceLocales
1281-
: OptionLocale | ResourceLocales
1278+
Locales = Locale extends GeneratedLocale
1279+
? GeneratedLocale
1280+
: OptionLocale extends Locale
1281+
? IsNever<ResourceLocales> extends true
1282+
? Locale
1283+
: ResourceLocales
1284+
: OptionLocale | ResourceLocales
12821285
> extends ComposerCustom {
12831286
/**
12841287
* @remarks

packages/vue-i18n-core/src/legacy.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ import type {
4646
PickupPaths,
4747
PickupFormatPathKeys,
4848
IsEmptyObject,
49-
IsNever
49+
IsNever,
50+
GeneratedLocale
5051
} from '@intlify/core-base'
5152
import type { VueDevToolsEmitter } from '@intlify/devtools-types'
5253
import type {
@@ -1047,11 +1048,13 @@ export interface VueI18n<
10471048
| PickupLocales<NonNullable<Messages>>
10481049
| PickupLocales<NonNullable<DateTimeFormats>>
10491050
| PickupLocales<NonNullable<NumberFormats>>,
1050-
Locales = OptionLocale extends string
1051-
? [ResourceLocales] extends [never]
1052-
? Locale
1053-
: ResourceLocales
1054-
: OptionLocale | ResourceLocales,
1051+
Locales = Locale extends GeneratedLocale
1052+
? GeneratedLocale
1053+
: OptionLocale extends string
1054+
? [ResourceLocales] extends [never]
1055+
? Locale
1056+
: ResourceLocales
1057+
: OptionLocale | ResourceLocales,
10551058
Composition extends Composer<
10561059
Messages,
10571060
DateTimeFormats,

packages/vue-i18n/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export {
5252
MessageCompilerContext,
5353
CompileError,
5454
MessageContext,
55-
RemovedIndexResources
55+
RemovedIndexResources,
56+
GeneratedTypeConfig
5657
} from '@intlify/core-base'
5758
export {
5859
VueMessageType,

0 commit comments

Comments
 (0)