Skip to content

Commit

Permalink
fix: FilterSet union type
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Streicher authored and coronoro committed Nov 16, 2023
1 parent 4c4f8f5 commit 464bcb6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/test/conversion-union.test.ts
Original file line number Diff line number Diff line change
@@ -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<Data> = {
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<Data> = {
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<Data> = {
number: {internal: {value: 123}},
}
const converted = convertFilterSetConfig(simpleConfig)
// eslint-disable-next-line camelcase
expect(converted).toEqual({number__internal: 123})
})
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export type FilterSetConfig<D = Record<any, any>, K extends FSKeyConfig<D> | nul
: CheckConfigKeys<D, Exclude<K, null>, key, C> // check if key is inside the config
) | (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
D[key] extends (Record<any, any> | undefined) ? // check if the type of the key is a Record, so we add extended references
Extract<object, D[key]> extends (Record<any, any> | undefined) ? // check if the type of the key is a Record, so we add extended references
(key extends keyof K ?
FilterSetConfig<D[key], K[key]>
: FilterSetConfig<D[key]>)
Expand Down

0 comments on commit 464bcb6

Please sign in to comment.