core-plugin-api: special handling of count for TranslationFunction

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-09-15 17:06:05 +02:00
parent 0f83314498
commit e43544013c
2 changed files with 23 additions and 3 deletions
@@ -23,6 +23,8 @@ describe('TranslationFunction', () => {
const f = (() => {}) as TranslationFunction<{
key_one: 'one';
key_other: 'other';
thingCount_one: '{{count}} thing';
thingCount_other: '{{count}} things';
foo: 'foo';
}>;
expect(f).toBeDefined();
@@ -30,6 +32,7 @@ describe('TranslationFunction', () => {
f('foo');
// @ts-expect-error
f('foo', { count: 1 });
f('key', { count: 1 });
// @ts-expect-error
f('key');
@@ -44,6 +47,20 @@ describe('TranslationFunction', () => {
// @ts-expect-error
f('key_other', { count: 6 });
f('thingCount', { count: 1 });
// @ts-expect-error
f('thingCount');
// @ts-expect-error
f('thingCount', { notCount: 1 });
// @ts-expect-error
f('thingCount_one');
// @ts-expect-error
f('thingCount_one', { count: 1 });
// @ts-expect-error
f('thingCount_other');
// @ts-expect-error
f('thingCount_other', { count: 6 });
const x1: 'one' | 'other' = f('key', { count: 6 });
// @ts-expect-error
const x2: 'one' = f('key', { count: 6 });
@@ -248,11 +248,14 @@ type CollectOptions<
TCount extends { count?: number },
TFormats extends {},
> = TCount &
(keyof TFormats extends never
// count is special, omit it from the replacements
(keyof Omit<TFormats, 'count'> extends never
? {}
: (
| Expand<ReplaceOptionsFromFormats<TFormats>>
| { replace: Expand<ReplaceOptionsFromFormats<TFormats>> }
| Expand<Omit<ReplaceOptionsFromFormats<TFormats>, 'count'>>
| {
replace: Expand<Omit<ReplaceOptionsFromFormats<TFormats>, 'count'>>;
}
) & {
formatParams?: Expand<ReplaceFormatParamsFromFormats<TFormats>>;
});