Skip to content

Commit 90969de

Browse files
committed
fix(typing): n() & d() output depending "part" option
1 parent b9109c0 commit 90969de

File tree

3 files changed

+55
-60
lines changed

3 files changed

+55
-60
lines changed

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

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ export interface CustomBlock<Message = VueMessageType> {
242242

243243
export type CustomBlocks<Message = VueMessageType> = Array<CustomBlock<Message>>
244244

245+
type IsPart<O> = O extends { part: infer P } ? P : false
246+
245247
// prettier-ignore
246248
/**
247249
* Composer Options
@@ -1113,17 +1115,14 @@ export interface ComposerDateTimeFormatting<
11131115
*/
11141116
<
11151117
Value extends number | Date | string = number,
1116-
Key extends string = string,
1117-
Return extends string | Intl.DateTimeFormatPart[] =
1118-
| string
1119-
| Intl.DateTimeFormatPart[]
1118+
Key extends string = string
11201119
>(
11211120
value: Value,
11221121
keyOrOptions:
11231122
| Key
11241123
| ResourceKeys
11251124
| DateTimeOptions<Key | ResourceKeys, Locales>
1126-
): Return
1125+
): IsPart<typeof keyOrOptions> extends true ? Intl.DateTimeFormatPart[] : string
11271126
/**
11281127
* Datetime formatting
11291128
*
@@ -1140,18 +1139,15 @@ export interface ComposerDateTimeFormatting<
11401139
*/
11411140
<
11421141
Value extends number | Date | string = number,
1143-
Key extends string = string,
1144-
Return extends string | Intl.DateTimeFormatPart[] =
1145-
| string
1146-
| Intl.DateTimeFormatPart[]
1142+
Key extends string = string
11471143
>(
11481144
value: Value,
11491145
keyOrOptions:
11501146
| Key
11511147
| ResourceKeys
11521148
| DateTimeOptions<Key | ResourceKeys, Locales>,
11531149
locale: Locales
1154-
): Return
1150+
): IsPart<typeof keyOrOptions> extends true ? Intl.DateTimeFormatPart[] : string
11551151
}
11561152

11571153
/**
@@ -1215,16 +1211,17 @@ export interface ComposerNumberFormatting<
12151211
*/
12161212
<
12171213
Key extends string = string,
1218-
Return extends string | Intl.NumberFormatPart[] =
1219-
| string
1220-
| Intl.NumberFormatPart[]
1221-
>(
1222-
value: number,
1223-
keyOrOptions:
1214+
OptionsType extends
1215+
| Key
1216+
| ResourceKeys
1217+
| NumberOptions<Key | ResourceKeys, Locales> =
12241218
| Key
12251219
| ResourceKeys
12261220
| NumberOptions<Key | ResourceKeys, Locales>
1227-
): Return
1221+
>(
1222+
value: number,
1223+
keyOrOptions: OptionsType
1224+
): IsPart<OptionsType> extends true ? Intl.NumberFormatPart[] : string
12281225
/**
12291226
* Number Formatting
12301227
*
@@ -1241,17 +1238,18 @@ export interface ComposerNumberFormatting<
12411238
*/
12421239
<
12431240
Key extends string = string,
1244-
Return extends string | Intl.NumberFormatPart[] =
1245-
| string
1246-
| Intl.NumberFormatPart[]
1247-
>(
1248-
value: number,
1249-
keyOrOptions:
1241+
OptionsType extends
12501242
| Key
12511243
| ResourceKeys
1252-
| NumberOptions<Key | ResourceKeys, Locales>,
1244+
| NumberOptions<Key | ResourceKeys, Locales> =
1245+
| Key
1246+
| ResourceKeys
1247+
| NumberOptions<Key | ResourceKeys, Locales>
1248+
>(
1249+
value: number,
1250+
keyOrOptions: OptionsType,
12531251
locale: Locales
1254-
): Return
1252+
): IsPart<OptionsType> extends true ? Intl.NumberFormatPart[] : string
12551253
}
12561254

12571255
/**

packages/vue-i18n-core/test/composer.test-d.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
DateTimeFormat,
77
FallbackLocales,
88
NumberFormat,
9+
NumberOptions,
910
PickupFallbackLocales
1011
} from '@intlify/core-base'
1112
import type { MyDatetimeScehma, MyNumberSchema, ResourceSchema } from './schema'
@@ -342,48 +343,48 @@ test('strict composer with direct options', () => {
342343
expectTypeOf(strictDirectComposer.numberFormats.value).toEqualTypeOf<{
343344
ca: { currency: { style: 'currency'; currencyDisplay: 'symbol' } }
344345
}>()
346+
345347
expectTypeOf(strictDirectComposer.d(new Date())).toEqualTypeOf<string>()
346348
expectTypeOf(
347-
strictDirectComposer.d<Date, string, string>(new Date(), 'short', 'ja-JP')
349+
strictDirectComposer.d<Date, string>(new Date(), 'short', 'ja-JP')
348350
).toEqualTypeOf<string>()
349351
expectTypeOf(
350352
strictDirectComposer.d(new Date(), { key: 'short', locale: 'zh' })
351-
).toEqualTypeOf<string | Intl.DateTimeFormatPart[]>()
353+
).toEqualTypeOf<string>()
352354
expectTypeOf(
353-
strictDirectComposer.d<Date, string, Intl.DateTimeFormatPart[]>(
354-
new Date(),
355-
{
356-
key: 'short',
357-
locale: 'zh',
358-
part: true
359-
}
360-
)
355+
strictDirectComposer.d<Date, string>(new Date(), {
356+
key: 'short',
357+
locale: 'zh',
358+
part: true
359+
})
361360
).toEqualTypeOf<Intl.DateTimeFormatPart[]>()
362361
expectTypeOf(
363-
strictDirectComposer.d<Date, string, string>(new Date(), 'custom' as any)
362+
strictDirectComposer.d<Date, string>(new Date(), 'custom' as any)
364363
).toEqualTypeOf<string>()
364+
365365
expectTypeOf(strictDirectComposer.n(1)).toEqualTypeOf<string>()
366-
expectTypeOf(strictDirectComposer.n(1, 'currency', 'zh')).toEqualTypeOf<
367-
string | Intl.NumberFormatPart[]
368-
>()
369366
expectTypeOf(
370-
strictDirectComposer.n<string, string>(1, 'currency', 'zh')
367+
strictDirectComposer.n(1, 'currency', 'zh')
368+
).toEqualTypeOf<string>()
369+
expectTypeOf(
370+
strictDirectComposer.n<string>(1, 'currency', 'zh')
371+
).toEqualTypeOf<string>()
372+
expectTypeOf(
373+
strictDirectComposer.n(1, { key: 'currency', locale: 'en', part: false })
371374
).toEqualTypeOf<string>()
372375
expectTypeOf(
373376
strictDirectComposer.n(1, { key: 'currency', locale: 'en', part: true })
374-
).toEqualTypeOf<string | Intl.NumberFormatPart[]>()
377+
).toEqualTypeOf<Intl.NumberFormatPart[]>()
375378
expectTypeOf(
376-
strictDirectComposer.n<string, Intl.NumberFormatPart[]>(1, {
379+
strictDirectComposer.n<string>(1, {
377380
key: 'currency',
378381
locale: 'en',
379382
part: true
380383
})
381384
).toEqualTypeOf<Intl.NumberFormatPart[]>()
382-
expectTypeOf(strictDirectComposer.n(1, 'currency')).toEqualTypeOf<
383-
string | Intl.NumberFormatPart[]
384-
>()
385+
expectTypeOf(strictDirectComposer.n(1, 'currency')).toEqualTypeOf<string>()
385386
expectTypeOf(
386-
strictDirectComposer.n<string, string>(1, 'currency')
387+
strictDirectComposer.n<string>(1, 'currency')
387388
).toEqualTypeOf<string>()
388389

389390
// const noOptionsComposer = createComposer({ missingWarn: true })

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import type {
2626

2727
import type { JsonPaths } from '@intlify/core-base'
2828

29+
type IsPart<O> = O extends { part: infer P } ? P : false
30+
2931
declare module 'vue' {
3032
/**
3133
* Component Custom Options for Vue I18n
@@ -602,9 +604,7 @@ declare module 'vue' {
602604
$d<
603605
Value extends number | Date = number,
604606
Key extends string = string,
605-
Return extends string | Intl.DateTimeFormatPart[] =
606-
| string
607-
| Intl.DateTimeFormatPart[],
607+
OptionsType = DateTimeOptions<Key | ResourceKeys>,
608608
DefinedDateTimeFormat extends
609609
RemovedIndexResources<DefineDateTimeFormat> = RemovedIndexResources<DefineDateTimeFormat>,
610610
Keys = IsEmptyObject<DefinedDateTimeFormat> extends false
@@ -617,7 +617,7 @@ declare module 'vue' {
617617
value: Value,
618618
options: DateTimeOptions<Key | ResourceKeys>,
619619
locale: Locale
620-
): Return
620+
): IsPart<OptionsType> extends true ? Intl.DateTimeFormatPart[] : string
621621
/**
622622
* Number formatting
623623
*
@@ -670,9 +670,7 @@ declare module 'vue' {
670670
*/
671671
$n<
672672
Key extends string = string,
673-
Return extends string | Intl.NumberFormatPart[] =
674-
| string
675-
| Intl.NumberFormatPart[],
673+
OptionsType = NumberOptions<Key | ResourceKeys>,
676674
DefinedNumberFormat extends
677675
RemovedIndexResources<DefineDateTimeFormat> = RemovedIndexResources<DefineDateTimeFormat>,
678676
Keys = IsEmptyObject<DefinedNumberFormat> extends false
@@ -683,8 +681,8 @@ declare module 'vue' {
683681
ResourceKeys extends Keys = IsNever<Keys> extends false ? Keys : never
684682
>(
685683
value: number,
686-
options: NumberOptions<Key | ResourceKeys>
687-
): Return
684+
options: OptionsType
685+
): IsPart<OptionsType> extends true ? Intl.NumberFormatPart[] : string
688686
/**
689687
* Number formatting
690688
*
@@ -726,9 +724,7 @@ declare module 'vue' {
726724
*/
727725
$n<
728726
Key extends string = string,
729-
Return extends string | Intl.NumberFormatPart[] =
730-
| string
731-
| Intl.NumberFormatPart[],
727+
OptionsType = NumberOptions<Key | ResourceKeys>,
732728
DefinedNumberFormat extends
733729
RemovedIndexResources<DefineDateTimeFormat> = RemovedIndexResources<DefineDateTimeFormat>,
734730
Keys = IsEmptyObject<DefinedNumberFormat> extends false
@@ -739,9 +735,9 @@ declare module 'vue' {
739735
ResourceKeys extends Keys = IsNever<Keys> extends false ? Keys : never
740736
>(
741737
value: number,
742-
options: NumberOptions<Key | ResourceKeys>,
738+
options: OptionsType,
743739
locale: Locale
744-
): Return
740+
): IsPart<OptionsType> extends true ? Intl.NumberFormatPart[] : string
745741

746742
/**
747743
* Locale messages getter

0 commit comments

Comments
 (0)