Skip to content

Commit

Permalink
fix(core): ensure strict type-checking of enum queries
Browse files Browse the repository at this point in the history
The `FilterQuery` type mistakenly allowed any string values in place of an enum.

This fix now enforces correct values, which also for TS `enum` means using the string
value is no longer possible:

```ts
// this will fail if `type` is a TS enum (but will work with a string union)
await em.find(User, { type: 'admin' });
```
  • Loading branch information
B4nan committed May 20, 2023
1 parent 66b3246 commit 8f8464a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/typings.ts
Expand Up @@ -61,7 +61,7 @@ export type IPrimaryKey<T extends IPrimaryKeyValue = IPrimaryKeyValue> = T;
export type Scalar = boolean | number | string | bigint | symbol | Date | RegExp | Buffer | { toHexString(): string };

export type ExpandScalar<T> = null | (T extends string
? string | RegExp
? T | RegExp
: T extends Date
? Date | string
: T);
Expand Down
38 changes: 38 additions & 0 deletions tests/types.test.ts
Expand Up @@ -324,6 +324,44 @@ describe('check typings', () => {
ok01 = { rels: ['abc'] };
});

test('FilterQuery with ', async () => {
enum ABC {
A,
B,
C,
}

enum DEF {
D = 'd',
E = 'e',
F = 'f',
}

interface Publisher {
id: string;
enum1: 'a' | 'b' | 'c';
enum2: ABC;
enum3: DEF;
}

let query: FilterQuery<Publisher>;
query = { enum1: 'a' };
query = { enum1: 'b' };
// @ts-expect-error
query = { enum1: 'd' };
query = { enum2: ABC.A };
// @ts-expect-error
query = { enum2: 'a' };
query = { enum2: ABC.A };
// @ts-expect-error
query = { enum2: DEF.D };
// @ts-expect-error
query = { enum3: 'd' };
// @ts-expect-error
query = { enum3: ABC.A };
query = { enum3: DEF.D };
});

test('AutoPath with optional nullable properties', async () => {
interface MessageRecipient {
id: string;
Expand Down

0 comments on commit 8f8464a

Please sign in to comment.