diff --git a/src/test/conversion-union.test.ts b/src/test/conversion-union.test.ts new file mode 100644 index 0000000..e6e6f37 --- /dev/null +++ b/src/test/conversion-union.test.ts @@ -0,0 +1,37 @@ +import type {FilterSetConfig} from '../types' +import {convertFilterSetConfig} from '../middleware' + +interface SpecialNumber { + internal: number +} + +interface Data { + number: number | SpecialNumber +} + +test('it should convert a primitive of a union type', () => { + const simpleConfig: FilterSetConfig = { + number: {lt: 123}, + } + const converted = convertFilterSetConfig(simpleConfig) + // eslint-disable-next-line camelcase + expect(converted).toEqual({number__lt: 123}) +}) + +test('it should convert a complex type of a union type', () => { + const special = {internal: 123} + const simpleConfig: FilterSetConfig = { + number: {value: special}, + } + const converted = convertFilterSetConfig(simpleConfig) + expect(converted).toEqual({number: special}) +}) + +test('the config should be able to filter attributes of complex types', () => { + const simpleConfig: FilterSetConfig = { + number: {internal: {value: 123}}, + } + const converted = convertFilterSetConfig(simpleConfig) + // eslint-disable-next-line camelcase + expect(converted).toEqual({number__internal: 123}) +}) diff --git a/src/types.ts b/src/types.ts index d926cf4..51d7099 100644 --- a/src/types.ts +++ b/src/types.ts @@ -120,7 +120,7 @@ export type FilterSetConfig, K extends FSKeyConfig | nul : CheckConfigKeys, key, C> // check if key is inside the config ) | ( // eslint-disable-next-line @typescript-eslint/no-explicit-any - D[key] extends (Record | undefined) ? // check if the type of the key is a Record, so we add extended references + 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)