Skip to content

Commit

Permalink
fix: Disallow string dates, improve autocomplete (#1557)
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn authored Nov 18, 2024
1 parent dfa5ea0 commit 9ea117c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
4 changes: 1 addition & 3 deletions packages/use-intl/src/core/TranslationValues.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type {ReactNode} from 'react';

// These type names are shown to consumers in autocomplete
export type ICUArg = string | number | boolean | Date;
export type ICUNumber = number;
export type ICUDate = Date | number | string;
// ^ Keep this in sync with `ICUArgument` in `createTranslator.tsx`

export type TranslationValues = Record<string, ICUArg>;

Expand Down
22 changes: 22 additions & 0 deletions packages/use-intl/src/core/createTranslator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,28 @@ describe('type safety', () => {
t('msg', obj);
});

it('validates numbers', () => {
const t = translateMessage('Percentage: {value, number, percent}');
t('msg', {value: 1.5});

// eslint-disable-next-line @typescript-eslint/no-unused-expressions
() => {
// @ts-expect-error
t('msg', {value: 'test'});
};
});

it('validates dates', () => {
const t = translateMessage('Date: {date, date, full}');
t('msg', {date: new Date('2024-07-09T07:06:03.320Z')});

// eslint-disable-next-line @typescript-eslint/no-unused-expressions
() => {
// @ts-expect-error
t('msg', {date: '2024-07-09T07:06:03.320Z'});
};
});

it('validates cardinal plurals', () => {
const t = translateMessage(
'You have {count, plural, =0 {no followers yet} =1 {one follower} other {# followers}}.'
Expand Down
11 changes: 6 additions & 5 deletions packages/use-intl/src/core/createTranslator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import type {
} from './MessageKeys.tsx';
import type {
ICUArg,
ICUDate,
ICUNumber,
MarkupTagsFunction,
RichTagsFunction
} from './TranslationValues.tsx';
Expand All @@ -33,9 +31,12 @@ type ICUArgsWithTags<
> = ICUArgs<
MessageString,
{
ICUArgument: ICUArg;
ICUNumberArgument: ICUNumber;
ICUDateArgument: ICUDate;
// Provide types inline instead of an alias so the
// consumer can see the types instead of the alias
ICUArgument: string | number | boolean | Date;
// ^ Keep this in sync with `ICUArg` in `TranslationValues.tsx`
ICUNumberArgument: number;
ICUDateArgument: Date | number;
}
> &
([TagsFn] extends [never] ? {} : ICUTags<MessageString, TagsFn>);
Expand Down
4 changes: 1 addition & 3 deletions packages/use-intl/src/core/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
export type {default as AbstractIntlMessages} from './AbstractIntlMessages.tsx';
export type {
TranslationValues,
ICUArg,
RichTranslationValues,
MarkupTranslationValues,
ICUArg,
ICUNumber,
ICUDate,
RichTagsFunction,
MarkupTagsFunction
} from './TranslationValues.tsx';
Expand Down

0 comments on commit 9ea117c

Please sign in to comment.