Skip to content

Commit

Permalink
fix: extract type and tests
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 464bcb6 commit 369a78e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/test/type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ interface Data {
new?: boolean
}

test('Error when type of attribute is passed without filter', () => {
const simpleConfig: FilterSetConfig<Data> = {
// @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<Data> = {
// @ts-expect-error number is not part of data type
Expand Down
19 changes: 13 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,19 @@ export type FilterSetConfig<D = Record<any, any>, K extends FSKeyConfig<D> | nul
FilterSet<D[key]> // no config so we take the default combinations for each key
: CheckConfigKeys<D, Exclude<K, null>, key, C> // check if key is inside the config
) | (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
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]>)
: never // no types added otherwise
Extract<D[key], object> 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<D[key], object> 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<Extract<D[key], object>, K[key]>
: FilterSetConfig<Extract<D[key], object>>
)
: never // no types added otherwise
)
)
}

Expand Down

0 comments on commit 369a78e

Please sign in to comment.