From 49529cb87b67b5bd93bfd5fb6aa3e1a3995fdcd2 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Tue, 27 May 2025 23:46:45 +0900 Subject: [PATCH] fix: n() & d() output depending "part" option --- packages/vue-i18n-core/src/composer.ts | 66 ++++++++++-------- .../vue-i18n-core/test/composer.test-d.ts | 68 +++++++++++-------- packages/vue-i18n/src/vue.d.ts | 18 +++-- 3 files changed, 82 insertions(+), 70 deletions(-) diff --git a/packages/vue-i18n-core/src/composer.ts b/packages/vue-i18n-core/src/composer.ts index 9569e1c07..f84e85b7e 100644 --- a/packages/vue-i18n-core/src/composer.ts +++ b/packages/vue-i18n-core/src/composer.ts @@ -242,6 +242,8 @@ export interface CustomBlock { export type CustomBlocks = Array> +type IsPart = O extends { part: infer P } ? P : false + // prettier-ignore /** * Composer Options @@ -1116,16 +1118,17 @@ export interface ComposerDateTimeFormatting< < Value extends number | Date | string = number, Key extends string = string, - Return extends string | Intl.DateTimeFormatPart[] = - | string - | Intl.DateTimeFormatPart[] - >( - value: Value, - keyOrOptions: + OptionsType extends + | Key + | ResourceKeys + | DateTimeOptions = | Key | ResourceKeys | DateTimeOptions - ): Return + >( + value: Value, + keyOrOptions: OptionsType + ): IsPart extends true ? Intl.DateTimeFormatPart[] : string /** * Datetime formatting * @@ -1143,17 +1146,18 @@ export interface ComposerDateTimeFormatting< < Value extends number | Date | string = number, Key extends string = string, - Return extends string | Intl.DateTimeFormatPart[] = - | string - | Intl.DateTimeFormatPart[] - >( - value: Value, - keyOrOptions: + OptionsType extends + | Key + | ResourceKeys + | DateTimeOptions = | Key | ResourceKeys - | DateTimeOptions, + | DateTimeOptions + >( + value: Value, + keyOrOptions: OptionsType, locale: Locales - ): Return + ): IsPart extends true ? Intl.DateTimeFormatPart[] : string } /** @@ -1217,16 +1221,17 @@ export interface ComposerNumberFormatting< */ < Key extends string = string, - Return extends string | Intl.NumberFormatPart[] = - | string - | Intl.NumberFormatPart[] - >( - value: number, - keyOrOptions: + OptionsType extends + | Key + | ResourceKeys + | NumberOptions = | Key | ResourceKeys | NumberOptions - ): Return + >( + value: number, + keyOrOptions: OptionsType + ): IsPart extends true ? Intl.NumberFormatPart[] : string /** * Number Formatting * @@ -1243,17 +1248,18 @@ export interface ComposerNumberFormatting< */ < Key extends string = string, - Return extends string | Intl.NumberFormatPart[] = - | string - | Intl.NumberFormatPart[] - >( - value: number, - keyOrOptions: + OptionsType extends + | Key + | ResourceKeys + | NumberOptions = | Key | ResourceKeys - | NumberOptions, + | NumberOptions + >( + value: number, + keyOrOptions: OptionsType, locale: Locales - ): Return + ): IsPart extends true ? Intl.NumberFormatPart[] : string } /** diff --git a/packages/vue-i18n-core/test/composer.test-d.ts b/packages/vue-i18n-core/test/composer.test-d.ts index 281a8fe6b..4f2c3bf94 100644 --- a/packages/vue-i18n-core/test/composer.test-d.ts +++ b/packages/vue-i18n-core/test/composer.test-d.ts @@ -342,55 +342,63 @@ test('strict composer with direct options', () => { expectTypeOf(strictDirectComposer.numberFormats.value).toEqualTypeOf<{ ca: { currency: { style: 'currency'; currencyDisplay: 'symbol' } } }>() + + // ComposerDateTimeFormatting expectTypeOf(strictDirectComposer.d(new Date())).toEqualTypeOf() expectTypeOf( - strictDirectComposer.d(new Date(), 'short', 'ja-JP') + strictDirectComposer.d(new Date(), 'short', 'ja-JP') ).toEqualTypeOf() expectTypeOf( strictDirectComposer.d(new Date(), { key: 'short', locale: 'zh' }) - ).toEqualTypeOf() + ).toEqualTypeOf() expectTypeOf( - strictDirectComposer.d( - new Date(), - { - key: 'short', - locale: 'zh', - part: true - } - ) - ).toEqualTypeOf() + strictDirectComposer.d(new Date(), 'custom' as any) + ).toEqualTypeOf() expectTypeOf( - strictDirectComposer.d(new Date(), 'custom' as any) + strictDirectComposer.d(new Date(), { + key: 'short', + locale: 'zh' + //part: undefined + }) ).toEqualTypeOf() - expectTypeOf(strictDirectComposer.n(1)).toEqualTypeOf() - expectTypeOf(strictDirectComposer.n(1, 'currency', 'zh')).toEqualTypeOf< - string | Intl.NumberFormatPart[] - >() expectTypeOf( - strictDirectComposer.n(1, 'currency', 'zh') + strictDirectComposer.d(new Date(), { + key: 'short', + locale: 'zh', + part: false + }) ).toEqualTypeOf() expectTypeOf( - strictDirectComposer.n(1, { key: 'currency', locale: 'en' }) + strictDirectComposer.d(new Date(), { + key: 'short', + locale: 'zh', + part: true + }) + ).toEqualTypeOf() + + // ComposerNumberFormatting + expectTypeOf(strictDirectComposer.n(1)).toEqualTypeOf() + expectTypeOf( + strictDirectComposer.n(1, 'currency', 'zh') ).toEqualTypeOf() + expectTypeOf(strictDirectComposer.n(1, 'currency')).toEqualTypeOf() expectTypeOf( - strictDirectComposer.n(1, { key: 'currency', locale: 'en' }) + strictDirectComposer.n(1, 'currency') ).toEqualTypeOf() + // part & return type expectTypeOf( - strictDirectComposer.n(1, { + strictDirectComposer.n(1, { key: 'currency', - locale: 'en', - part: true + locale: 'en' + //part: undefined }) - ).toEqualTypeOf() - expectTypeOf(strictDirectComposer.n(1, 'currency')).toEqualTypeOf< - string | Intl.NumberFormatPart[] - >() + ).toEqualTypeOf() expectTypeOf( - strictDirectComposer.n(1, 'currency') + strictDirectComposer.n(1, { key: 'currency', locale: 'en', part: false }) ).toEqualTypeOf() - expectTypeOf(strictDirectComposer.n(1, 'custom' as any)).toEqualTypeOf< - string | Intl.NumberFormatPart[] - >() + expectTypeOf( + strictDirectComposer.n(1, { key: 'currency', locale: 'en', part: true }) + ).toEqualTypeOf() // const noOptionsComposer = createComposer({ missingWarn: true }) const noOptionsComposer = createComposer({ locale: 'en' }) diff --git a/packages/vue-i18n/src/vue.d.ts b/packages/vue-i18n/src/vue.d.ts index db1a29baa..a8143c519 100644 --- a/packages/vue-i18n/src/vue.d.ts +++ b/packages/vue-i18n/src/vue.d.ts @@ -31,6 +31,8 @@ import type { VueI18nOptions } from '../../vue-i18n-core/src/legacy' +type IsPart = O extends { part: infer P } ? P : false + // --- THE CONTENT BELOW THIS LINE WILL BE APPENDED TO DTS FILE IN DIST DIRECTORY --- // declare module 'vue' { /** @@ -674,9 +676,7 @@ declare module 'vue' { $d< value extends number | Date = number, Key extends string = string, - Return extends string | Intl.DateTimeFormatPart[] = - | string - | Intl.DateTimeFormatPart[], + OptionsType = DateTimeOptions, DefinedDateTimeFormat extends RemovedIndexResources = RemovedIndexResources, Keys = IsEmptyObject extends false @@ -687,8 +687,8 @@ declare module 'vue' { ResourceKeys extends Keys = IsNever extends false ? Keys : never >( value: number | Date, - options: DateTimeOptions - ): Return + options: OptionsType + ): IsPart extends true ? Intl.DateTimeFormatPart[] : string /** * Number formatting * @@ -884,9 +884,7 @@ declare module 'vue' { */ $n< Key extends string, - Return extends string | Intl.NumberFormatPart[] = - | string - | Intl.NumberFormatPart[], + OptionsType = NumberOptions, DefinedNumberFormat extends RemovedIndexResources = RemovedIndexResources, Keys = IsEmptyObject extends false @@ -897,8 +895,8 @@ declare module 'vue' { ResourceKeys extends Keys = IsNever extends false ? Keys : never >( value: number, - options: NumberOptions - ): Return + options: OptionsType + ): IsPart extends true ? Intl.NumberFormatPart[] : string /** * Locale messages getter *