Skip to content

Commit

Permalink
feat(@formatjs/intl): support TS4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Feb 25, 2021
1 parent 6cd5718 commit ba56b9a
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/intl/src/dateTime.ts
Expand Up @@ -26,7 +26,6 @@ const DATE_TIME_FORMAT_OPTIONS: Array<keyof DateTimeFormatOptions> = [
'hourCycle',
'dateStyle',
'timeStyle',
'fractionalSecondDigits',
'calendar',
// 'dayPeriod',
'numberingSystem',
Expand Down Expand Up @@ -57,6 +56,7 @@ export function getFormatter(
let filteredOptions = filterProps(
options,
DATE_TIME_FORMAT_OPTIONS,
// @ts-expect-error es2020 has a lot stuff from es2021 bleed in
defaults
);

Expand Down
12 changes: 4 additions & 8 deletions packages/intl/src/relativeTime.ts
Expand Up @@ -3,13 +3,9 @@ import {IntlFormatters, Formatters, CustomFormats, OnErrorFn} from './types';
import {getNamedFormat, filterProps} from './utils';
import {FormatError, ErrorCode} from 'intl-messageformat';
import {MessageFormatError} from './error';
import {
IntlRelativeTimeFormatOptions,
RelativeTimeFormat,
} from '@formatjs/ecma402-abstract';

const RELATIVE_TIME_FORMAT_OPTIONS: Array<
keyof IntlRelativeTimeFormatOptions
keyof Intl.RelativeTimeFormatOptions
> = ['numeric', 'style'];

function getFormatter(
Expand All @@ -24,15 +20,15 @@ function getFormatter(
},
getRelativeTimeFormat: Formatters['getRelativeTimeFormat'],
options: Parameters<IntlFormatters['formatRelativeTime']>[2] = {}
): RelativeTimeFormat {
): Intl.RelativeTimeFormat {
const {format} = options;

const defaults =
(!!format && getNamedFormat(formats, 'relative', format, onError)) || {};
const filteredOptions = filterProps(
options,
RELATIVE_TIME_FORMAT_OPTIONS,
defaults as IntlRelativeTimeFormatOptions
defaults as Intl.RelativeTimeFormatOptions
);

return getRelativeTimeFormat(locale, filteredOptions);
Expand All @@ -52,7 +48,7 @@ export function formatRelativeTime(
if (!unit) {
unit = 'second';
}
const RelativeTimeFormat = (Intl as any).RelativeTimeFormat;
const RelativeTimeFormat = Intl.RelativeTimeFormat;
if (!RelativeTimeFormat) {
config.onError(
new FormatError(
Expand Down
16 changes: 7 additions & 9 deletions packages/intl/src/types.ts
Expand Up @@ -6,7 +6,6 @@ import {
FormatError,
Options as IntlMessageFormatOptions,
} from 'intl-messageformat';
import IntlRelativeTimeFormat from '@formatjs/intl-relativetimeformat';
import {DateTimeFormat} from '@formatjs/ecma402-abstract';
import {MessageFormatElement} from 'intl-messageformat-parser';
import IntlListFormat, {IntlListFormatOptions} from '@formatjs/intl-listformat';
Expand All @@ -20,7 +19,6 @@ import {
} from './error';
import {DEFAULT_INTL_CONFIG} from './utils';
import {
IntlRelativeTimeFormatOptions,
DateTimeFormatOptions,
NumberFormatOptions,
} from '@formatjs/ecma402-abstract';
Expand Down Expand Up @@ -52,7 +50,7 @@ export interface IntlConfig<T = string> {
}

export interface CustomFormats extends Partial<Formats> {
relative?: Record<string, IntlRelativeTimeFormatOptions>;
relative?: Record<string, Intl.RelativeTimeFormatOptions>;
}

export interface CustomFormatConfig {
Expand All @@ -70,7 +68,7 @@ export type FormatNumberOptions = Exclude<
> &
CustomFormatConfig;
export type FormatRelativeTimeOptions = Exclude<
IntlRelativeTimeFormatOptions,
Intl.RelativeTimeFormatOptions,
'localeMatcher'
> &
CustomFormatConfig;
Expand Down Expand Up @@ -110,8 +108,8 @@ export interface IntlFormatters<T = any, R = T> {
opts?: FormatDateOptions
): Intl.DateTimeFormatPart[];
formatRelativeTime(
value: Parameters<IntlRelativeTimeFormat['format']>[0],
unit?: Parameters<IntlRelativeTimeFormat['format']>[1],
value: Parameters<Intl.RelativeTimeFormat['format']>[0],
unit?: Parameters<Intl.RelativeTimeFormat['format']>[1],
opts?: FormatRelativeTimeOptions
): string;
formatNumber(
Expand Down Expand Up @@ -158,8 +156,8 @@ export interface Formatters {
...args: ConstructorParameters<typeof IntlMessageFormat>
): IntlMessageFormat;
getRelativeTimeFormat(
...args: ConstructorParameters<typeof IntlRelativeTimeFormat>
): IntlRelativeTimeFormat;
...args: ConstructorParameters<typeof Intl.RelativeTimeFormat>
): Intl.RelativeTimeFormat;
getPluralRules(
...args: ConstructorParameters<typeof Intl.PluralRules>
): Intl.PluralRules;
Expand All @@ -179,7 +177,7 @@ export interface IntlCache {
dateTime: Record<string, DateTimeFormat>;
number: Record<string, Intl.NumberFormat>;
message: Record<string, IntlMessageFormat>;
relativeTime: Record<string, IntlRelativeTimeFormat>;
relativeTime: Record<string, Intl.RelativeTimeFormat>;
pluralRules: Record<string, Intl.PluralRules>;
list: Record<string, IntlListFormat>;
displayNames: Record<string, DisplayNames>;
Expand Down
7 changes: 2 additions & 5 deletions packages/intl/src/utils.ts
Expand Up @@ -9,10 +9,7 @@ import {IntlMessageFormat} from 'intl-messageformat';
import * as memoize from 'fast-memoize';
import {Cache} from 'fast-memoize';
import {UnsupportedFormatterError} from './error';
import {
DateTimeFormat,
IntlRelativeTimeFormatOptions,
} from '@formatjs/ecma402-abstract';
import {DateTimeFormat} from '@formatjs/ecma402-abstract';

export function filterProps<T extends Record<string, any>, K extends string>(
props: T,
Expand Down Expand Up @@ -165,7 +162,7 @@ export function getNamedFormat<T extends keyof CustomFormats>(
):
| Intl.NumberFormatOptions
| Intl.DateTimeFormatOptions
| IntlRelativeTimeFormatOptions
| Intl.RelativeTimeFormatOptions
| undefined {
const formatType = formats && formats[type];
let format;
Expand Down
3 changes: 2 additions & 1 deletion packages/intl/tests/formatDate.test.ts
Expand Up @@ -107,6 +107,7 @@ describe('format API', () => {
});

it('falls back and warns on invalid Intl.DateTimeFormat options', () => {
// @ts-expect-error invalid year just for testing
expect(formatDate(0, {year: 'invalid'})).toBe('0');
expect(
(config.onError as jest.Mock).mock.calls.map(c => c[0].code)
Expand All @@ -125,7 +126,7 @@ describe('format API', () => {

it('uses named formats as defaults', () => {
const date = new Date();
const opts = {month: 'numeric'};
const opts: Intl.DateTimeFormatOptions = {month: 'numeric'};
const format = 'year-only';

const {locale, formats} = config;
Expand Down
7 changes: 0 additions & 7 deletions packages/intl/tests/formatRelativeTime.test.ts
@@ -1,13 +1,6 @@
/* eslint-disable @typescript-eslint/camelcase */
import {formatRelativeTime as formatRelativeTimeFn} from '../src/relativeTime';
import {OptionalIntlConfig, IntlFormatters} from '../src/types';
import IntlRelativeTimeFormat from '@formatjs/intl-relativetimeformat';

declare global {
namespace Intl {
const RelativeTimeFormat: typeof IntlRelativeTimeFormat;
}
}

describe('format API', () => {
const {NODE_ENV} = process.env;
Expand Down
3 changes: 2 additions & 1 deletion packages/intl/tests/formatTime.test.ts
Expand Up @@ -138,6 +138,7 @@ describe('format API', () => {
});

it('falls back and warns on invalid Intl.DateTimeFormat options', () => {
// @ts-expect-error just for test
expect(formatTime(0, {hour: 'invalid'})).toBe('0');
expect(
(config.onError as jest.Mock).mock.calls.map(c => c[0].code)
Expand All @@ -156,7 +157,7 @@ describe('format API', () => {

it('uses named formats as defaults', () => {
const date = new Date();
const opts = {minute: '2-digit'};
const opts: Intl.DateTimeFormatOptions = {minute: '2-digit'};
const format = 'hour-only';

const {locale, formats} = config;
Expand Down

0 comments on commit ba56b9a

Please sign in to comment.