Skip to content

Commit 49529cb

Browse files
committed
fix: n() & d() output depending "part" option
1 parent 7ef7285 commit 49529cb

File tree

3 files changed

+82
-70
lines changed

3 files changed

+82
-70
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
@@ -1116,16 +1118,17 @@ export interface ComposerDateTimeFormatting<
11161118
<
11171119
Value extends number | Date | string = number,
11181120
Key extends string = string,
1119-
Return extends string | Intl.DateTimeFormatPart[] =
1120-
| string
1121-
| Intl.DateTimeFormatPart[]
1122-
>(
1123-
value: Value,
1124-
keyOrOptions:
1121+
OptionsType extends
1122+
| Key
1123+
| ResourceKeys
1124+
| DateTimeOptions<Key | ResourceKeys, Locales> =
11251125
| Key
11261126
| ResourceKeys
11271127
| DateTimeOptions<Key | ResourceKeys, Locales>
1128-
): Return
1128+
>(
1129+
value: Value,
1130+
keyOrOptions: OptionsType
1131+
): IsPart<OptionsType> extends true ? Intl.DateTimeFormatPart[] : string
11291132
/**
11301133
* Datetime formatting
11311134
*
@@ -1143,17 +1146,18 @@ export interface ComposerDateTimeFormatting<
11431146
<
11441147
Value extends number | Date | string = number,
11451148
Key extends string = string,
1146-
Return extends string | Intl.DateTimeFormatPart[] =
1147-
| string
1148-
| Intl.DateTimeFormatPart[]
1149-
>(
1150-
value: Value,
1151-
keyOrOptions:
1149+
OptionsType extends
1150+
| Key
1151+
| ResourceKeys
1152+
| DateTimeOptions<Key | ResourceKeys, Locales> =
11521153
| Key
11531154
| ResourceKeys
1154-
| DateTimeOptions<Key | ResourceKeys, Locales>,
1155+
| DateTimeOptions<Key | ResourceKeys, Locales>
1156+
>(
1157+
value: Value,
1158+
keyOrOptions: OptionsType,
11551159
locale: Locales
1156-
): Return
1160+
): IsPart<OptionsType> extends true ? Intl.DateTimeFormatPart[] : string
11571161
}
11581162

11591163
/**
@@ -1217,16 +1221,17 @@ export interface ComposerNumberFormatting<
12171221
*/
12181222
<
12191223
Key extends string = string,
1220-
Return extends string | Intl.NumberFormatPart[] =
1221-
| string
1222-
| Intl.NumberFormatPart[]
1223-
>(
1224-
value: number,
1225-
keyOrOptions:
1224+
OptionsType extends
1225+
| Key
1226+
| ResourceKeys
1227+
| NumberOptions<Key | ResourceKeys, Locales> =
12261228
| Key
12271229
| ResourceKeys
12281230
| NumberOptions<Key | ResourceKeys, Locales>
1229-
): Return
1231+
>(
1232+
value: number,
1233+
keyOrOptions: OptionsType
1234+
): IsPart<OptionsType> extends true ? Intl.NumberFormatPart[] : string
12301235
/**
12311236
* Number Formatting
12321237
*
@@ -1243,17 +1248,18 @@ export interface ComposerNumberFormatting<
12431248
*/
12441249
<
12451250
Key extends string = string,
1246-
Return extends string | Intl.NumberFormatPart[] =
1247-
| string
1248-
| Intl.NumberFormatPart[]
1249-
>(
1250-
value: number,
1251-
keyOrOptions:
1251+
OptionsType extends
1252+
| Key
1253+
| ResourceKeys
1254+
| NumberOptions<Key | ResourceKeys, Locales> =
12521255
| Key
12531256
| ResourceKeys
1254-
| NumberOptions<Key | ResourceKeys, Locales>,
1257+
| NumberOptions<Key | ResourceKeys, Locales>
1258+
>(
1259+
value: number,
1260+
keyOrOptions: OptionsType,
12551261
locale: Locales
1256-
): Return
1262+
): IsPart<OptionsType> extends true ? Intl.NumberFormatPart[] : string
12571263
}
12581264

12591265
/**

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

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -342,55 +342,63 @@ 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>()
362357
expectTypeOf(
363-
strictDirectComposer.d<Date, string, string>(new Date(), 'custom' as any)
358+
strictDirectComposer.d(new Date(), {
359+
key: 'short',
360+
locale: 'zh'
361+
//part: undefined
362+
})
364363
).toEqualTypeOf<string>()
365-
expectTypeOf(strictDirectComposer.n(1)).toEqualTypeOf<string>()
366-
expectTypeOf(strictDirectComposer.n(1, 'currency', 'zh')).toEqualTypeOf<
367-
string | Intl.NumberFormatPart[]
368-
>()
369364
expectTypeOf(
370-
strictDirectComposer.n<string, string>(1, 'currency', 'zh')
365+
strictDirectComposer.d(new Date(), {
366+
key: 'short',
367+
locale: 'zh',
368+
part: false
369+
})
371370
).toEqualTypeOf<string>()
372371
expectTypeOf(
373-
strictDirectComposer.n<string, string>(1, { key: 'currency', locale: 'en' })
372+
strictDirectComposer.d(new Date(), {
373+
key: 'short',
374+
locale: 'zh',
375+
part: true
376+
})
377+
).toEqualTypeOf<Intl.DateTimeFormatPart[]>()
378+
379+
// ComposerNumberFormatting
380+
expectTypeOf(strictDirectComposer.n(1)).toEqualTypeOf<string>()
381+
expectTypeOf(
382+
strictDirectComposer.n<string, string>(1, 'currency', 'zh')
374383
).toEqualTypeOf<string>()
384+
expectTypeOf(strictDirectComposer.n(1, 'currency')).toEqualTypeOf<string>()
375385
expectTypeOf(
376-
strictDirectComposer.n<string, string>(1, { key: 'currency', locale: 'en' })
386+
strictDirectComposer.n<string>(1, 'currency')
377387
).toEqualTypeOf<string>()
388+
// part & return type
378389
expectTypeOf(
379-
strictDirectComposer.n<string, Intl.NumberFormatPart[]>(1, {
390+
strictDirectComposer.n(1, {
380391
key: 'currency',
381-
locale: 'en',
382-
part: true
392+
locale: 'en'
393+
//part: undefined
383394
})
384-
).toEqualTypeOf<Intl.NumberFormatPart[]>()
385-
expectTypeOf(strictDirectComposer.n(1, 'currency')).toEqualTypeOf<
386-
string | Intl.NumberFormatPart[]
387-
>()
395+
).toEqualTypeOf<string>()
388396
expectTypeOf(
389-
strictDirectComposer.n<string, string>(1, 'currency')
397+
strictDirectComposer.n(1, { key: 'currency', locale: 'en', part: false })
390398
).toEqualTypeOf<string>()
391-
expectTypeOf(strictDirectComposer.n(1, 'custom' as any)).toEqualTypeOf<
392-
string | Intl.NumberFormatPart[]
393-
>()
399+
expectTypeOf(
400+
strictDirectComposer.n(1, { key: 'currency', locale: 'en', part: true })
401+
).toEqualTypeOf<Intl.NumberFormatPart[]>()
394402

395403
// const noOptionsComposer = createComposer({ missingWarn: true })
396404
const noOptionsComposer = createComposer({ locale: 'en' })

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import type {
3131
VueI18nOptions
3232
} from '../../vue-i18n-core/src/legacy'
3333

34+
type IsPart<O> = O extends { part: infer P } ? P : false
35+
3436
// --- THE CONTENT BELOW THIS LINE WILL BE APPENDED TO DTS FILE IN DIST DIRECTORY --- //
3537
declare module 'vue' {
3638
/**
@@ -674,9 +676,7 @@ declare module 'vue' {
674676
$d<
675677
value extends number | Date = number,
676678
Key extends string = string,
677-
Return extends string | Intl.DateTimeFormatPart[] =
678-
| string
679-
| Intl.DateTimeFormatPart[],
679+
OptionsType = DateTimeOptions<Key | ResourceKeys>,
680680
DefinedDateTimeFormat extends
681681
RemovedIndexResources<DefineDateTimeFormat> = RemovedIndexResources<DefineDateTimeFormat>,
682682
Keys = IsEmptyObject<DefinedDateTimeFormat> extends false
@@ -687,8 +687,8 @@ declare module 'vue' {
687687
ResourceKeys extends Keys = IsNever<Keys> extends false ? Keys : never
688688
>(
689689
value: number | Date,
690-
options: DateTimeOptions<Key, ResourceKeys>
691-
): Return
690+
options: OptionsType
691+
): IsPart<OptionsType> extends true ? Intl.DateTimeFormatPart[] : string
692692
/**
693693
* Number formatting
694694
*
@@ -884,9 +884,7 @@ declare module 'vue' {
884884
*/
885885
$n<
886886
Key extends string,
887-
Return extends string | Intl.NumberFormatPart[] =
888-
| string
889-
| Intl.NumberFormatPart[],
887+
OptionsType = NumberOptions<Key | ResourceKeys>,
890888
DefinedNumberFormat extends
891889
RemovedIndexResources<DefineDateTimeFormat> = RemovedIndexResources<DefineDateTimeFormat>,
892890
Keys = IsEmptyObject<DefinedNumberFormat> extends false
@@ -897,8 +895,8 @@ declare module 'vue' {
897895
ResourceKeys extends Keys = IsNever<Keys> extends false ? Keys : never
898896
>(
899897
value: number,
900-
options: NumberOptions<Key, ResourceKeys>
901-
): Return
898+
options: OptionsType
899+
): IsPart<OptionsType> extends true ? Intl.NumberFormatPart[] : string
902900
/**
903901
* Locale messages getter
904902
*

0 commit comments

Comments
 (0)