Skip to content

Commit 32b2c9b

Browse files
littleboarx双面胶
andauthored
improve types declaration (#897)
* improve types declaration * improve i18n types declaration Co-authored-by: 双面胶 <[email protected]>
1 parent 95db8b2 commit 32b2c9b

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ export function createI18n<
311311
>(
312312
options: Options,
313313
LegacyVueI18n?: any // eslint-disable-line @typescript-eslint/no-explicit-any
314-
): I18n<Messages, DateTimeFormats, NumberFormats, OptionLocale, Legacy>
314+
): typeof options['legacy'] extends true
315+
? I18n<Messages, DateTimeFormats, NumberFormats, OptionLocale, true>
316+
: typeof options['legacy'] extends false
317+
? I18n<Messages, DateTimeFormats, NumberFormats, OptionLocale, false>
318+
: I18n<Messages, DateTimeFormats, NumberFormats, OptionLocale, Legacy>
315319

316320
/**
317321
* Vue I18n factory
@@ -410,7 +414,11 @@ export function createI18n<
410414
>(
411415
options: Options,
412416
LegacyVueI18n?: any // eslint-disable-line @typescript-eslint/no-explicit-any
413-
): I18n<Messages, DateTimeFormats, NumberFormats, OptionLocale, Legacy>
417+
): typeof options['legacy'] extends true
418+
? I18n<Messages, DateTimeFormats, NumberFormats, OptionLocale, true>
419+
: typeof options['legacy'] extends false
420+
? I18n<Messages, DateTimeFormats, NumberFormats, OptionLocale, false>
421+
: I18n<Messages, DateTimeFormats, NumberFormats, OptionLocale, Legacy>
414422

415423
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
416424
export function createI18n(options: any = {}, VueI18nLegacy?: any): any {

test-d/vue-i18n/i18n.test-d.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,26 @@ expectType<
264264
LocaleParams<'en' | 'ja'>
265265
>
266266
>(strictOptions)
267+
// check loose i18n composer
268+
const looseI18nComposer = createI18n({
269+
...looseOptions,
270+
legacy: false
271+
}).global
272+
expectType<'en' | 'ja' | 'en-US' | 'ja-JP'>(looseI18nComposer.locale.value)
273+
expectType<{
274+
en: {
275+
foo: string
276+
nest: {
277+
bar: string
278+
}
279+
}
280+
ja: {
281+
bar: string
282+
nest: {
283+
bar: string
284+
}
285+
}
286+
}>(looseI18nComposer.messages.value)
267287

268288
// check loose i18n
269289
const looseI18n = createI18n(looseOptions).global
@@ -376,7 +396,7 @@ looseI18n.mergeNumberFormat<{ echoes: { act: string } }>('ja-JP', {
376396
})
377397

378398
// check strict i18n
379-
const strictI18n = createI18n<[ResourceSchema], 'en' | 'ja', false>(
399+
const strictI18n = createI18n<[ResourceSchema], 'en' | 'ja'>(
380400
strictOptions
381401
).global
382402
expectType<'en' | 'ja'>(strictI18n.locale.value)

0 commit comments

Comments
 (0)