Skip to content

Commit 9ea117c

Browse files
authored
fix: Disallow string dates, improve autocomplete (#1557)
1 parent dfa5ea0 commit 9ea117c

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

packages/use-intl/src/core/TranslationValues.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type {ReactNode} from 'react';
22

3-
// These type names are shown to consumers in autocomplete
43
export type ICUArg = string | number | boolean | Date;
5-
export type ICUNumber = number;
6-
export type ICUDate = Date | number | string;
4+
// ^ Keep this in sync with `ICUArgument` in `createTranslator.tsx`
75

86
export type TranslationValues = Record<string, ICUArg>;
97

packages/use-intl/src/core/createTranslator.test.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,28 @@ describe('type safety', () => {
244244
t('msg', obj);
245245
});
246246

247+
it('validates numbers', () => {
248+
const t = translateMessage('Percentage: {value, number, percent}');
249+
t('msg', {value: 1.5});
250+
251+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
252+
() => {
253+
// @ts-expect-error
254+
t('msg', {value: 'test'});
255+
};
256+
});
257+
258+
it('validates dates', () => {
259+
const t = translateMessage('Date: {date, date, full}');
260+
t('msg', {date: new Date('2024-07-09T07:06:03.320Z')});
261+
262+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
263+
() => {
264+
// @ts-expect-error
265+
t('msg', {date: '2024-07-09T07:06:03.320Z'});
266+
};
267+
});
268+
247269
it('validates cardinal plurals', () => {
248270
const t = translateMessage(
249271
'You have {count, plural, =0 {no followers yet} =1 {one follower} other {# followers}}.'

packages/use-intl/src/core/createTranslator.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import type {
1212
} from './MessageKeys.tsx';
1313
import type {
1414
ICUArg,
15-
ICUDate,
16-
ICUNumber,
1715
MarkupTagsFunction,
1816
RichTagsFunction
1917
} from './TranslationValues.tsx';
@@ -33,9 +31,12 @@ type ICUArgsWithTags<
3331
> = ICUArgs<
3432
MessageString,
3533
{
36-
ICUArgument: ICUArg;
37-
ICUNumberArgument: ICUNumber;
38-
ICUDateArgument: ICUDate;
34+
// Provide types inline instead of an alias so the
35+
// consumer can see the types instead of the alias
36+
ICUArgument: string | number | boolean | Date;
37+
// ^ Keep this in sync with `ICUArg` in `TranslationValues.tsx`
38+
ICUNumberArgument: number;
39+
ICUDateArgument: Date | number;
3940
}
4041
> &
4142
([TagsFn] extends [never] ? {} : ICUTags<MessageString, TagsFn>);

packages/use-intl/src/core/index.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
export type {default as AbstractIntlMessages} from './AbstractIntlMessages.tsx';
22
export type {
33
TranslationValues,
4+
ICUArg,
45
RichTranslationValues,
56
MarkupTranslationValues,
6-
ICUArg,
7-
ICUNumber,
8-
ICUDate,
97
RichTagsFunction,
108
MarkupTagsFunction
119
} from './TranslationValues.tsx';

0 commit comments

Comments
 (0)