From 369a78ec358631daddbf709bf189e8b9546d0899 Mon Sep 17 00:00:00 2001 From: Tim Streicher Date: Thu, 9 Nov 2023 14:29:40 +0100 Subject: [PATCH] fix: extract type and tests --- src/test/type.test.ts | 8 ++++++++ src/types.ts | 19 +++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/test/type.test.ts b/src/test/type.test.ts index 8eb16d8..f537c82 100644 --- a/src/test/type.test.ts +++ b/src/test/type.test.ts @@ -12,6 +12,14 @@ interface Data { new?: boolean } +test('Error when type of attribute is passed without filter', () => { + const simpleConfig: FilterSetConfig = { + // @ts-expect-error plain passing is not allowed + text: 'string', + } + expect(() => convertFilterSetConfig(simpleConfig)).toThrow('Cannot use \'in\' operator to search for \'value\' in string') +}) + test('Error when attribute is not in data type', () => { const simpleConfig: FilterSetConfig = { // @ts-expect-error number is not part of data type diff --git a/src/types.ts b/src/types.ts index 51d7099..af08394 100644 --- a/src/types.ts +++ b/src/types.ts @@ -119,12 +119,19 @@ export type FilterSetConfig, K extends FSKeyConfig | nul FilterSet // no config so we take the default combinations for each key : CheckConfigKeys, key, C> // check if key is inside the config ) | ( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Extract extends (Record | undefined) ? // check if the type of the key is a Record, so we add extended references - (key extends keyof K ? - FilterSetConfig - : FilterSetConfig) - : never // no types added otherwise + Extract extends never ? // check if the type D[key] consist only of primitives + never // primitives are handled by value + : + (// there is a non-primitve part + // eslint-disable-next-line @typescript-eslint/no-explicit-any + Extract extends (Record | undefined) ? // check if the type of the key is a Record, so we add extended references + ( + key extends keyof K ? + FilterSetConfig, K[key]> + : FilterSetConfig> + ) + : never // no types added otherwise + ) ) }