diff --git a/packages/vue-i18n-core/src/composer.ts b/packages/vue-i18n-core/src/composer.ts index 19005b320..31840666c 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 @@ -1114,16 +1116,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 * @@ -1141,17 +1144,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 } /** @@ -1215,16 +1219,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 * @@ -1241,17 +1246,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 581395542..d30cafeb3 100644 --- a/packages/vue-i18n-core/test/composer.test-d.ts +++ b/packages/vue-i18n-core/test/composer.test-d.ts @@ -342,49 +342,67 @@ 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() + // part & return type expectTypeOf( - strictDirectComposer.d(new Date(), 'custom' as any) + strictDirectComposer.d(new Date(), { + key: 'short', + locale: 'zh' + //part: undefined + }) ).toEqualTypeOf() + expectTypeOf( + strictDirectComposer.d(new Date(), { + key: 'short', + locale: 'zh', + part: false + }) + ).toEqualTypeOf() + expectTypeOf( + strictDirectComposer.d(new Date(), { + key: 'short', + locale: 'zh', + part: true + }) + ).toEqualTypeOf() + + // ComposerNumberFormatting expectTypeOf(strictDirectComposer.n(1)).toEqualTypeOf() - expectTypeOf(strictDirectComposer.n(1, 'currency', 'zh')).toEqualTypeOf< - string | Intl.NumberFormatPart[] - >() expectTypeOf( - strictDirectComposer.n(1, 'currency', 'zh') + strictDirectComposer.n(1, 'currency', 'zh') ).toEqualTypeOf() expectTypeOf( - strictDirectComposer.n(1, { key: 'currency', locale: 'en', part: true }) - ).toEqualTypeOf() + strictDirectComposer.n(1, 'currency', 'zh') + ).toEqualTypeOf() + expectTypeOf(strictDirectComposer.n(1, 'currency')).toEqualTypeOf() expectTypeOf( - strictDirectComposer.n(1, { + strictDirectComposer.n(1, 'currency') + ).toEqualTypeOf() + // part & return type + expectTypeOf( + 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, { 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 c322a1f09..f8acd94f8 100644 --- a/packages/vue-i18n/src/vue.d.ts +++ b/packages/vue-i18n/src/vue.d.ts @@ -26,6 +26,8 @@ import type { import type { JsonPaths } from '@intlify/core-base' +type IsPart = O extends { part: infer P } ? P : false + declare module 'vue' { /** * Component Custom Options for Vue I18n @@ -602,9 +604,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 @@ -615,9 +615,9 @@ declare module 'vue' { ResourceKeys extends Keys = IsNever extends false ? Keys : never >( value: Value, - options: DateTimeOptions, + options: OptionsType, locale: Locale - ): Return + ): IsPart extends true ? Intl.DateTimeFormatPart[] : string /** * Number formatting * @@ -670,9 +670,7 @@ declare module 'vue' { */ $n< Key extends string = string, - Return extends string | Intl.NumberFormatPart[] = - | string - | Intl.NumberFormatPart[], + OptionsType = NumberOptions, DefinedNumberFormat extends RemovedIndexResources = RemovedIndexResources, Keys = IsEmptyObject extends false @@ -683,8 +681,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 /** * Number formatting * @@ -726,9 +724,7 @@ declare module 'vue' { */ $n< Key extends string = string, - Return extends string | Intl.NumberFormatPart[] = - | string - | Intl.NumberFormatPart[], + OptionsType = NumberOptions, DefinedNumberFormat extends RemovedIndexResources = RemovedIndexResources, Keys = IsEmptyObject extends false @@ -739,9 +735,9 @@ declare module 'vue' { ResourceKeys extends Keys = IsNever extends false ? Keys : never >( value: number, - options: NumberOptions, + options: OptionsType, locale: Locale - ): Return + ): IsPart extends true ? Intl.NumberFormatPart[] : string /** * Locale messages getter