Skip to content

Commit

Permalink
⚡ improvement(types): typed autocomplete in date and number format op…
Browse files Browse the repository at this point in the history
…tions (#485) by @Raiondesu

* ⚡improvement(types): typed autocomplete in date and number format options

Replace Number and Date format options with standard TS `Intl` types, while also adding guiding TS autocomplete to them.

This change exterminates the confusion of which types to follow, while preserving backwards-compatibility for types and adding optional autocomplete.

* ⚡improvement(types/test): add type constraints to format options

It's useful to check static variables types whenever possible.
  • Loading branch information
Raiondesu authored and kazupon committed Dec 17, 2018
1 parent ef4b1a6 commit e2e5993
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
70 changes: 43 additions & 27 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,55 @@ declare namespace VueI18n {
interface LocaleMessageArray { [index: number]: LocaleMessage; }
interface LocaleMessages { [key: string]: LocaleMessageObject; }
type TranslateResult = string | LocaleMessages;
interface DateTimeFormatOptions {
year?: string;
month?: string;
day?: string;
hour?: string;
minute?: string;
second?: string;
weekday?: string;
hour12?: boolean;
era?: string;
timeZone?: string;
timeZoneName?: string;
localeMatcher?: string;
formatMatcher?: string;

type LocaleMatcher = 'lookup' | 'best-fit';
type FormatMatcher = 'basic' | 'best-fit';

type DateTimeHumanReadable = 'long' | 'short' | 'narrow';
type DateTimeDigital = 'numeric' | '2-digit';

interface SpecificDateTimeFormatOptions extends Intl.DateTimeFormatOptions {
year?: DateTimeDigital;
month?: DateTimeDigital | DateTimeHumanReadable;
day?: DateTimeDigital;
hour?: DateTimeDigital;
minute?: DateTimeDigital;
second?: DateTimeDigital;
weekday?: DateTimeHumanReadable;
era?: DateTimeHumanReadable;
timeZoneName?: 'long' | 'short';
localeMatcher?: LocaleMatcher;
formatMatcher?: FormatMatcher;
}

type DateTimeFormatOptions = Intl.DateTimeFormatOptions | SpecificDateTimeFormatOptions;

interface DateTimeFormat { [key: string]: DateTimeFormatOptions; }
interface DateTimeFormats { [key: string]: DateTimeFormat; }
interface DateTimeFormats { [locale: string]: DateTimeFormat; }
type DateTimeFormatResult = string;
interface NumberFormatOptions {
style?: string;

type CurrencyDisplay = 'symbol' | 'code' | 'name';

interface SpecificNumberFormatOptions extends Intl.NumberFormatOptions {
style?: 'decimal' | 'percent';
currency?: string;
currencyDisplay?: string;
useGrouping?: boolean;
minimumIntegerDigits?: number;
minimumFractionDigits?: number;
maximumFractionDigits?: number;
minimumSignificantDigits?: number;
maximumSignificantDigits?: number;
localeMatcher?: string;
formatMatcher?: string;
currencyDisplay?: CurrencyDisplay;
localeMatcher?: LocaleMatcher;
formatMatcher?: FormatMatcher;
}

interface CurrencyNumberFormatOptions extends Intl.NumberFormatOptions {
style: 'currency';
currency: string; // Obligatory if style is 'currency'
currencyDisplay?: CurrencyDisplay;
localeMatcher?: LocaleMatcher;
formatMatcher?: FormatMatcher;
}

type NumberFormatOptions = Intl.NumberFormatOptions | SpecificNumberFormatOptions | CurrencyNumberFormatOptions;

interface NumberFormat { [key: string]: NumberFormatOptions; }
interface NumberFormats { [key: string]: NumberFormat; }
interface NumberFormats { [locale: string]: NumberFormat; }
type NumberFormatResult = string;
type PluralizationRulesMap = {
/**
Expand Down
6 changes: 3 additions & 3 deletions types/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue, { ComponentOptions } from 'vue';
import VueI18n from "../index";
import VueI18n, { DateTimeFormatOptions, NumberFormatOptions } from "../index";

/*
import * as VueI18n from 'vue-i18n';
Expand Down Expand Up @@ -27,11 +27,11 @@ VueI18n.availabilities; // $ExpectType IntlAvailability
const locale = 'locale';
const key = 'key';
const value = 'value';
const dateTimeFormatOptions = {
const dateTimeFormatOptions: DateTimeFormatOptions = {
year: '2-digit',
timeZone: 'Asia/Tokyo'
};
const numberFormatOptions = {
const numberFormatOptions: NumberFormatOptions = {
style: 'currency',
currency: 'JPY'
};
Expand Down

0 comments on commit e2e5993

Please sign in to comment.