Skip to content

Commit d4c6e3a

Browse files
authored
fix(typing): n() & d() output depending "part" option (#2193)
* fix(typing): n() & d() output depending "part" option * refactor: remove tests
1 parent b9109c0 commit d4c6e3a

File tree

3 files changed

+91
-71
lines changed

3 files changed

+91
-71
lines changed

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

Lines changed: 36 additions & 30 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
@@ -1114,16 +1116,17 @@ export interface ComposerDateTimeFormatting<
11141116
<
11151117
Value extends number | Date | string = number,
11161118
Key extends string = string,
1117-
Return extends string | Intl.DateTimeFormatPart[] =
1118-
| string
1119-
| Intl.DateTimeFormatPart[]
1120-
>(
1121-
value: Value,
1122-
keyOrOptions:
1119+
OptionsType extends
1120+
| Key
1121+
| ResourceKeys
1122+
| DateTimeOptions<Key | ResourceKeys, Locales> =
11231123
| Key
11241124
| ResourceKeys
11251125
| DateTimeOptions<Key | ResourceKeys, Locales>
1126-
): Return
1126+
>(
1127+
value: Value,
1128+
keyOrOptions: OptionsType
1129+
): IsPart<OptionsType> extends true ? Intl.DateTimeFormatPart[] : string
11271130
/**
11281131
* Datetime formatting
11291132
*
@@ -1141,17 +1144,18 @@ export interface ComposerDateTimeFormatting<
11411144
<
11421145
Value extends number | Date | string = number,
11431146
Key extends string = string,
1144-
Return extends string | Intl.DateTimeFormatPart[] =
1145-
| string
1146-
| Intl.DateTimeFormatPart[]
1147-
>(
1148-
value: Value,
1149-
keyOrOptions:
1147+
OptionsType extends
1148+
| Key
1149+
| ResourceKeys
1150+
| DateTimeOptions<Key | ResourceKeys, Locales> =
11501151
| Key
11511152
| ResourceKeys
1152-
| DateTimeOptions<Key | ResourceKeys, Locales>,
1153+
| DateTimeOptions<Key | ResourceKeys, Locales>
1154+
>(
1155+
value: Value,
1156+
keyOrOptions: OptionsType,
11531157
locale: Locales
1154-
): Return
1158+
): IsPart<OptionsType> extends true ? Intl.DateTimeFormatPart[] : string
11551159
}
11561160

11571161
/**
@@ -1215,16 +1219,17 @@ export interface ComposerNumberFormatting<
12151219
*/
12161220
<
12171221
Key extends string = string,
1218-
Return extends string | Intl.NumberFormatPart[] =
1219-
| string
1220-
| Intl.NumberFormatPart[]
1221-
>(
1222-
value: number,
1223-
keyOrOptions:
1222+
OptionsType extends
1223+
| Key
1224+
| ResourceKeys
1225+
| NumberOptions<Key | ResourceKeys, Locales> =
12241226
| Key
12251227
| ResourceKeys
12261228
| NumberOptions<Key | ResourceKeys, Locales>
1227-
): Return
1229+
>(
1230+
value: number,
1231+
keyOrOptions: OptionsType
1232+
): IsPart<OptionsType> extends true ? Intl.NumberFormatPart[] : string
12281233
/**
12291234
* Number Formatting
12301235
*
@@ -1241,17 +1246,18 @@ export interface ComposerNumberFormatting<
12411246
*/
12421247
<
12431248
Key extends string = string,
1244-
Return extends string | Intl.NumberFormatPart[] =
1245-
| string
1246-
| Intl.NumberFormatPart[]
1247-
>(
1248-
value: number,
1249-
keyOrOptions:
1249+
OptionsType extends
1250+
| Key
1251+
| ResourceKeys
1252+
| NumberOptions<Key | ResourceKeys, Locales> =
12501253
| Key
12511254
| ResourceKeys
1252-
| NumberOptions<Key | ResourceKeys, Locales>,
1255+
| NumberOptions<Key | ResourceKeys, Locales>
1256+
>(
1257+
value: number,
1258+
keyOrOptions: OptionsType,
12531259
locale: Locales
1254-
): Return
1260+
): IsPart<OptionsType> extends true ? Intl.NumberFormatPart[] : string
12551261
}
12561262

12571263
/**

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

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -342,49 +342,67 @@ test('strict composer with direct options', () => {
342342
expectTypeOf(strictDirectComposer.numberFormats.value).toEqualTypeOf<{
343343
ca: { currency: { style: 'currency'; currencyDisplay: 'symbol' } }
344344
}>()
345+
346+
// ComposerDateTimeFormatting
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-
)
361-
).toEqualTypeOf<Intl.DateTimeFormatPart[]>()
355+
strictDirectComposer.d<Date, string>(new Date(), 'custom' as any)
356+
).toEqualTypeOf<string>()
357+
// part & return type
362358
expectTypeOf(
363-
strictDirectComposer.d<Date, string, string>(new Date(), 'custom' as any)
359+
strictDirectComposer.d(new Date(), {
360+
key: 'short',
361+
locale: 'zh'
362+
//part: undefined
363+
})
364364
).toEqualTypeOf<string>()
365+
expectTypeOf(
366+
strictDirectComposer.d(new Date(), {
367+
key: 'short',
368+
locale: 'zh',
369+
part: false
370+
})
371+
).toEqualTypeOf<string>()
372+
expectTypeOf(
373+
strictDirectComposer.d(new Date(), {
374+
key: 'short',
375+
locale: 'zh',
376+
part: true
377+
})
378+
).toEqualTypeOf<Intl.DateTimeFormatPart[]>()
379+
380+
// ComposerNumberFormatting
365381
expectTypeOf(strictDirectComposer.n(1)).toEqualTypeOf<string>()
366-
expectTypeOf(strictDirectComposer.n(1, 'currency', 'zh')).toEqualTypeOf<
367-
string | Intl.NumberFormatPart[]
368-
>()
369382
expectTypeOf(
370-
strictDirectComposer.n<string, string>(1, 'currency', 'zh')
383+
strictDirectComposer.n(1, 'currency', 'zh')
371384
).toEqualTypeOf<string>()
372385
expectTypeOf(
373-
strictDirectComposer.n(1, { key: 'currency', locale: 'en', part: true })
374-
).toEqualTypeOf<string | Intl.NumberFormatPart[]>()
386+
strictDirectComposer.n<string>(1, 'currency', 'zh')
387+
).toEqualTypeOf<string>()
388+
expectTypeOf(strictDirectComposer.n(1, 'currency')).toEqualTypeOf<string>()
375389
expectTypeOf(
376-
strictDirectComposer.n<string, Intl.NumberFormatPart[]>(1, {
390+
strictDirectComposer.n<string>(1, 'currency')
391+
).toEqualTypeOf<string>()
392+
// part & return type
393+
expectTypeOf(
394+
strictDirectComposer.n(1, {
377395
key: 'currency',
378-
locale: 'en',
379-
part: true
396+
locale: 'en'
397+
//part: undefined
380398
})
381-
).toEqualTypeOf<Intl.NumberFormatPart[]>()
382-
expectTypeOf(strictDirectComposer.n(1, 'currency')).toEqualTypeOf<
383-
string | Intl.NumberFormatPart[]
384-
>()
399+
).toEqualTypeOf<string>()
385400
expectTypeOf(
386-
strictDirectComposer.n<string, string>(1, 'currency')
401+
strictDirectComposer.n(1, { key: 'currency', locale: 'en', part: false })
387402
).toEqualTypeOf<string>()
403+
expectTypeOf(
404+
strictDirectComposer.n(1, { key: 'currency', locale: 'en', part: true })
405+
).toEqualTypeOf<Intl.NumberFormatPart[]>()
388406

389407
// const noOptionsComposer = createComposer({ missingWarn: true })
390408
const noOptionsComposer = createComposer({ locale: 'en' })

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

Lines changed: 11 additions & 15 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
@@ -615,9 +615,9 @@ declare module 'vue' {
615615
ResourceKeys extends Keys = IsNever<Keys> extends false ? Keys : never
616616
>(
617617
value: Value,
618-
options: DateTimeOptions<Key | ResourceKeys>,
618+
options: OptionsType,
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)