From e43544013cfd634b666c0950cd95f8d6df6c2976 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 15 Sep 2023 17:06:05 +0200 Subject: [PATCH] core-plugin-api: special handling of count for TranslationFunction Signed-off-by: Patrik Oldsberg --- .../src/apis/definitions/TranslationApi.test.ts | 17 +++++++++++++++++ .../src/apis/definitions/TranslationApi.ts | 9 ++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/core-plugin-api/src/apis/definitions/TranslationApi.test.ts b/packages/core-plugin-api/src/apis/definitions/TranslationApi.test.ts index e231659e38..ba5d43e896 100644 --- a/packages/core-plugin-api/src/apis/definitions/TranslationApi.test.ts +++ b/packages/core-plugin-api/src/apis/definitions/TranslationApi.test.ts @@ -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 }); diff --git a/packages/core-plugin-api/src/apis/definitions/TranslationApi.ts b/packages/core-plugin-api/src/apis/definitions/TranslationApi.ts index a8aa03ef72..3c5402e684 100644 --- a/packages/core-plugin-api/src/apis/definitions/TranslationApi.ts +++ b/packages/core-plugin-api/src/apis/definitions/TranslationApi.ts @@ -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 extends never ? {} : ( - | Expand> - | { replace: Expand> } + | Expand, 'count'>> + | { + replace: Expand, 'count'>>; + } ) & { formatParams?: Expand>; });